隨筆 - 25, 文章 - 1, 評論 - 0, 引用 - 0
          數據加載中……

          [20100427]-[源碼]-[Java]-[讀取文件并輸出]

          import java.io.BufferedReader;
          import java.io.BufferedWriter;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.InputStreamReader;
          import java.io.OutputStreamWriter;

          public class Main {

              
          /**
               * 
          @param args
               
          */

              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                  Main
                          .ListFilesInDirectory(
          "E://WorkShop/source");
              }


              
          static void ListFilesInDirectory(String path) {
                  File file 
          = new File(path);
                  File[] files 
          = file.listFiles();
                  
          for (File fl : files) {
                      
          if (fl.isDirectory())
                          ListFilesInDirectory(fl.toString());
                      
          else {
                          
          if (fl.getName().endsWith("java")) {
                              System.out.println(fl.getName());

                              
          try {
                                  Main.ReadFile(fl);
                              }
           catch (Exception e) {
                                  
          // TODO Auto-generated catch block
                                  e.printStackTrace();
                              }

                          }

                      }

                  }


              }


              
          public static void ReadFile(File f) throws Exception {
                  BufferedReader br 
          = new BufferedReader(new InputStreamReader(
                          
          new FileInputStream(f)));
                  BufferedWriter bw 
          = new BufferedWriter(new OutputStreamWriter(
                          
          new FileOutputStream(new File("d://output1.txt"), true)));
                  String tmp 
          = new String();
                  
          while ((tmp = br.readLine()) != null{
                      
          if (tmp.matches("\\s+"|| tmp.length() == 0)
                          
          continue;
                      
          if (tmp.matches("^\\s*(/\\*).*"|| tmp.matches("^\\s*\\*.*")||tmp.matches("^\\s*//.*")
                              
          || tmp.matches("^\\s+\\*/.*")){
                          
          continue;
                      }

                      bw.write(tmp);
                      bw.newLine();
                  }

                  br.close();
                  bw.close();
              }

          }

          posted on 2010-05-06 23:08 至尊貝貝 閱讀(161) 評論(0)  編輯  收藏 所屬分類: 代碼_Java


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黄浦区| 朝阳区| 彝良县| 建瓯市| 台山市| 齐河县| 澜沧| 县级市| 拜泉县| 威海市| 孝感市| 凤翔县| 沾益县| 姜堰市| 芦溪县| 景德镇市| 宜宾市| 普格县| 西峡县| 仁化县| 山东省| 教育| 阿克陶县| 本溪| 西峡县| 延庆县| 湛江市| 天祝| 福泉市| 文水县| 定陶县| 广平县| 安乡县| 黄陵县| 鄂尔多斯市| 永修县| 将乐县| 桂林市| 马龙县| 奉节县| 普安县|