用動作去驅動未來

          生命在于運動,讓自己身體的每一個細胞都動起來吧.

           

          java對指定目錄下文件的讀寫

          最近因為項目的國際化的需要,需要對整個項目的100來個插件做國際化,這是一件痛苦的事情,因為純體力勞動。為了省點工作量,想著能不能寫個程序批處理了,減少點工作量,于是就有了下面的代碼。

          1.讀取指定的(.java)文件:
          public static String readFile(String path) throws IOException {
                  File f = new File(path);
                  StringBuffer res = new StringBuffer();
                  String filePathStr = f.getPath();
                  System.out.println("獲取文件的路徑:::::::"+filePathStr);
                  
                  FileInputStream fis = new FileInputStream(f);
                  InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
                  BufferedReader br = new BufferedReader(isr, 8192 * 8);
                  
                  String line = null;
                  int linenum = 0;
                  while((line=br.readLine())!=null) {
                      linenum ++;
                      res.append(line+"此處可以添加你自己的字符串處理邏輯"+"\r\n");
                  }
                  br.close();
                  
                  return res.toString();
              }
          2.讀取的文件內容信息寫到指定的(.java)文件
          public static boolean writeFile(String cont, String path) {
                  try {
                      File dist = new File(path);
                      OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"GBK"); 
                      writer.write(cont);
                      writer.flush();
                      writer.close();
                      return true;
                  } catch (IOException e) {
                      e.printStackTrace();
                      return false;
                  }
              }
          3.查找指定目錄下所有符合條件的.java文件,并更新文件信息
              /**
               * 查找文件
               * @param f
               * @throws IOException
               */
              public static void findFile(File f) throws IOException {
                  if(f.exists()) {
                      if(f.isDirectory()) {
                          for(File fs:f.listFiles(ff)) {
                              findFile(fs);
                          }
                      } else {
                              updateFile(f);
                      }
                  }
              }
              
              /**
               * 逐行讀java文件
               * @param f
               * @throws IOException
               */
              private static void updateFile(File f) throws IOException {
                  String filePathStr = f.getPath();
                  System.out.println("開始讀取文件的路徑:::::::"+filePathStr);
                  FileInputStream fis = new FileInputStream(f);
                  InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
                  BufferedReader br = new BufferedReader(isr, 8192 * 8);
                  
                  String line = null;
                  int linenum = 0;
                  StringBuffer res = new StringBuffer();
                  while((line=br.readLine())!=null) {
                      String updateStr= updateStr(line);
                      res.append(updateStr+"\r\n");
                      
                      if(!line.trim().equals(updateStr.trim())) {
                          linenum ++;
                      }
                  }
                  br.close();
                  
                  //如果文件有修改,則修改后的文件,覆蓋原有文件
                  if(linenum>0) {
                      System.out.println("=============================");
                      System.out.println("filePathStr:"+filePathStr);
                      System.out.println("文件修改了:"+linenum+"處。");
                      System.out.println("=============================");
                      String cont = res.toString();
                      ReadWriteFile.write(cont, filePathStr);
                  }
              }
              
              /**
               * 驗證讀取的字符串信息
               * 和更新字符串信息
               * @param str
               */
              private static String updateStr(String str) {
                  //判斷字符串是否是需要更新的字符串
                  boolean isok = filterStr(str);
                  int strNum = StringValidation.strNum(str, StringValidation.ch);
                  if(isok || strNum == 0) {
                      return str;
                  } else {
                      String temp = ""; 
                      for(int i=1;i<=strNum/2;i++) {
                          temp += " //$NON-NLS-"+i+"$"; //需要添加的字符
                      }
                      str = str+temp;
                  }
                  return str;
              }
              
              //過濾文件類型
              private static FileFilter ff = new FileFilter() {
                  public boolean accept(File pathname) {
                      String path = pathname.getName().toLowerCase();
                      logger.info("FileFilter path::::"+path);
                      //只匹配 .java 結尾的文件
                      if (pathname.isDirectory() || path.endsWith(".java")) {
                          return true;
                      }
                      return false;
                  }
              };

              /**
               * 過濾掉不需要處理的字符串
               * @param str
               * @return
               */
              public static boolean filterStr(String str) {
                  boolean isok = false;
                  
                  //過濾字符串
                  isok = (str.indexOf("import ")>=0)
                          || (str.indexOf("package ")>=0)
                          || (str.indexOf(" class ")>=0)
                          || (str.indexOf("http://$NON-NLS")>=0)
                          || (str.indexOf("http://")==0)
                          || (str.indexOf("/*")>=0)
                          || (str.indexOf("*")>=0)
                          || (str.trim().indexOf("@")==0)
                          || (str.indexOf("\"")==-1)
                          || ("".equals(str))
                          || isCh(str);
                  return isok;
              }

              /**
               * 驗證字符串是否含有中文字符
               * @param str
               * @return
               */
              public static boolean isCh(String str) {
                  Pattern   pa   =   Pattern.compile("[\u4E00-\u9FA0]");
                  Matcher   m   =   pa.matcher(str);
                  boolean isok = m.find();
                  return isok;
              }

          總結:當我們拿到一個別人給的需求,先不要急于去處理,先分析,再分析,然后做出最優的解決方案,處理好這項工作。

          posted on 2012-11-23 15:32 黑螞蟻 閱讀(1946) 評論(0)  編輯  收藏 所屬分類: JAVA 基礎

          導航

          統計

          公告

          路在腳下,此刻,出發。

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 凉山| 南郑县| 景东| 武穴市| 焦作市| 云阳县| 濮阳县| 叙永县| 万载县| 青阳县| 崇信县| 杭锦后旗| 靖边县| 繁峙县| 通城县| 郧西县| 东乡县| 铁力市| 江津市| 沾化县| 南岸区| 从化市| 水富县| 无棣县| 延吉市| 全南县| 英超| 昔阳县| 宁安市| 黔西县| 太湖县| 苏州市| 云和县| 镇安县| 三明市| 平和县| 绥阳县| 松滋市| 独山县| 涞水县| 广宁县|