隨筆-77  評(píng)論-5  文章-2  trackbacks-0

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.FileReader;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.PushbackInputStream;
          import java.util.ArrayList;
          import java.util.HashSet;
          import java.util.List;
          import java.util.Set;

          public class UtfTool
          {

              /**
               * ant 編譯之后的result文件,注意要編譯提示錯(cuò)誤的文件名要在同一行
               * 可以設(shè)置命令提示窗口的緩沖區(qū)大小實(shí)現(xiàn)
               * @param resultFileName
               */
              public static Set getFileNamesFromCompileResult(String resultFileName)
               throws java.io.IOException
              {
           Set<String> set = new HashSet();
           BufferedReader reader = new BufferedReader(new FileReader(
            resultFileName));
           String start = "[javac] ";
           int startLen = start.length();
           String end = ".java:";
           int endLen = end.length();
           
           String errMsg = "\\65279";
           while (reader.ready())
           {
               String line = reader.readLine();
               int indexStart = line.indexOf(start);
              
               if(line.indexOf(errMsg) == -1)
               {
            continue;
               }
               if (indexStart != -1)
               {
            int indexEnd = line.indexOf(end);
            if (indexEnd != -1)
            {
                String name = line.substring(indexStart + startLen,
                 indexEnd + endLen - 1);
                set.add(name.trim());
            }
               }
           }
           return set;

              }
             
              /**
               * 讀取流中前面的字符,看是否有bom,如果有bom,將bom頭先讀掉丟棄
               * @param in
               * @return
               * @throws IOException
               */
              public static InputStream getInputStream(InputStream in) throws IOException
              {

           PushbackInputStream testin = new PushbackInputStream(in);
           int ch = testin.read();
           if (ch != 0xEF)
           {
               testin.unread(ch);
           } else if ((ch = testin.read()) != 0xBB)
           {
               testin.unread(ch);
               testin.unread(0xef);
           } else if ((ch = testin.read()) != 0xBF)
           {
               throw new IOException("錯(cuò)誤的UTF-8格式文件");
           } else
           {
               //不需要做,這里是bom頭被讀完了
              //// System.out.println("still exist bom");

           }
           return testin;

              }
             
             
             
              /**
               * 根據(jù)一個(gè)文件名,讀取完文件,干掉bom頭。
               * @param fileName
               * @throws IOException
               */
              public static void trimBom(String fileName) throws IOException
              {

           FileInputStream fin = new FileInputStream(fileName);
           //開始寫臨時(shí)文件
           InputStream in = getInputStream(fin);
           String tmpFileName = fileName + ".tmp";
           FileOutputStream out = new FileOutputStream(tmpFileName);
           byte b[] = new byte[4096];

           int len = 0;
           while (in.available() > 0)
           {
               len = in.read(b, 0, 4096);
               out.write(b, 0, len);
           }

           in.close();
           fin.close();
           out.close();

           //臨時(shí)文件寫完,開始將臨時(shí)文件寫回本文件。
           in = new FileInputStream(tmpFileName);
           System.out.println("[" + fileName + "]");
           out = new FileOutputStream(fileName);

           while (in.available() > 0)
           {
               len = in.read(b, 0, 4096);
               out.write(b, 0, len);
           }
           in.close();
           out.close();
              }

              /**
               * 根據(jù)ant編譯錯(cuò)誤來去除bom
               * @param resultFile
               * @throws IOException
               */
              static void trimBomByCompileResult(String resultFile) throws IOException
              {
           Set<String> set = getFileNamesFromCompileResult(resultFile);
           for (String fName : set)
           {
               trimBom(fName);
           }
              }

              public static void main(String[] args) throws IOException
              {
           trimBomByCompileResult("c:/result3.txt");
           
           ///List<String> list = getFileNamesFromCompileResult(resultFile);

          // InputStream in = new FileInputStream(
          //  "D:/cc/SSB_SRC_DEV_lijinglin2/TP_SSB/SSB_CJ203_Component/ProductCode/com/zte/ssb/component/access/implementation/dao/ImplementationDAO.java");
          // getInputStream2(in);
          // in.close();
              }
          }


          文章來源:http://stocknewbie.bokee.com/viewdiary.22061282.html
          posted on 2009-05-01 10:52 huohuo 閱讀(1864) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 额尔古纳市| 龙海市| 郎溪县| 通河县| 富顺县| 罗平县| 乐安县| 德化县| 长岛县| 正蓝旗| 巫溪县| 扎兰屯市| 合川市| 沙洋县| 中西区| 汉沽区| 聊城市| 镇巴县| 读书| 横峰县| 正阳县| 亚东县| 小金县| 仁怀市| 峨眉山市| 墨玉县| 囊谦县| 馆陶县| 留坝县| 沙坪坝区| 阳江市| 广东省| 浙江省| 赤城县| 格尔木市| 丹棱县| 平定县| 威信县| 安徽省| 柯坪县| 武城县|