BOMRemover v2 0 去除代码中的UTF-8 BOM

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

org.xml.sax.SAXParseException: 缺少文件根组件。但是XML文件确实存在且格式正常.


原因: XML文件可能为UTF-8编码....且带有BOM签名..用UltraEdit等16进制工具打开XML,可看到前三字节为EF,BB,BF.


 

解决方法..找到此XML文件,删除BOM签名...

 

见我的 BOM批量删除工具 BOMRemoverV2.0

 

此工具来源于BOMRemover 在此基础上完美了下方法...主要增加了 noBomFile方法 ,以检测文件是否为BOM签名文件.

 

使用方法:

1.trimBom   去掉单个文件的BOM头

2.DealSrcFiles 去掉某个文件夹下的文件BOM头...

 

  1. import java.io.BufferedReader;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.FileReader;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.PushbackInputStream;  
  10. import java.util.ArrayList;  
  11. import java.util.HashSet;  
  12. import java.util.Iterator;  
  13. import java.util.List;  
  14. import java.util.Set;  
  15.   
  16. public class BOMRemover {  
  17.   
  18.     /** 
  19.      * ant 编译之后的result文件,注意要编译提示错误的文件名要在同一行 可以设置命令提示窗口的缓冲区大小实现 
  20.      *  
  21.      * @param resultFileName 
  22.      */  
  23.     public static Set getFileNamesFromCompileResult(String resultFileName)  
  24.             throws java.io.IOException {  
  25.         Set set = new HashSet();  
  26.         BufferedReader reader = new BufferedReader(new FileReader(  
  27.                 resultFileName));  
  28.         String start = "[javac] ";  
  29.         int startLen = start.length();  
  30.         String end = ".java:";  
  31.         int endLen = end.length();  
  32.   
  33.         String errMsg = "//65279";  
  34.         while (reader.ready()) {  
  35.             String line = reader.readLine();  
  36.             int indexStart = line.indexOf(start);  
  37.   
  38.             if (line.indexOf(errMsg) == -1) {  
  39.                 continue;  
  40.             }  
  41.             if (indexStart != -1) {  
  42.                 int indexEnd = line.indexOf(end);  
  43.                 if (indexEnd != -1) {  
  44.                     String name = line.substring(indexStart + startLen,  
  45.                             indexEnd + endLen - 1);  
  46.                     set.add(name.trim());  
  47.                 }  
  48.             }  
  49.         }  
  50.         return set;  
  51.   
  52.     }  
  53.   
  54.       
  55.     //trim dir  
  56.     public static void DealSrcFiles(String path) {  
  57.         if (path.charAt(path.length() - 1) != '//') {  
  58.             path += '//';  
  59.         }  
  60.         File file = new File(path);  
  61.         if (!file.exists()) {  
  62.             System.out.println("Error: Path not Existed! Please Check it out!");  
  63.             return;  
  64.         }  
  65.         String[] filelist = file.list();  
  66.         for (int i = 0; i < filelist.length; i++) {  
  67.             File temp = new File(path + filelist[i]);  
  68.             if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {  
  69.                 DealSrcFiles(path + filelist[i]);  
  70.             } else {  
  71.                 if (filelist[i].endsWith(".xml")) {  
  72.                     try {  
  73.                         // System.out.println(path + filelist[i]);  
  74.                         trimBom(path + filelist[i]);  
  75.                     } catch (Exception eee) {  
  76.                         System.out.println(eee.getMessage());  
  77.                     }  
  78.                 }  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     /** 
  84.      * 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃 
  85.      *  
  86.      * @param in 
  87.      * @return 
  88.      * @throws IOException 
  89.      */  
  90.     public static InputStream getInputStream(InputStream in) throws IOException {  
  91.   
  92.         PushbackInputStream testin = new PushbackInputStream(in);  
  93.         int ch = testin.read();  
  94.         if (ch != 0xEF) {  
  95.             testin.unread(ch);  
  96.         } else if ((ch = testin.read()) != 0xBB) { // if ch==0xef  
  97.             testin.unread(ch);  
  98.             testin.unread(0xef);  
  99.         } else if ((ch = testin.read()) != 0xBF) { // if ch ==0xbb  
  100.             throw new IOException("错误的UTF-8格式文件");  
  101.         } else { // if ch ==0xbf  
  102.             // 不需要做,这里是bom头被读完了  
  103.             // // System.out.println("still exist bom");  
  104.   
  105.         }  
  106.         return testin;  
  107.   
  108.     }  
  109.   
  110.     /** 
  111.      * 根据一个文件名,读取完文件,干掉bom头。 
  112.      *  
  113.      * @param fileName 
  114.      * @throws IOException 
  115.      */  
  116.     public static void trimBom(String fileName) throws IOException {  
  117.   
  118.       
  119.   
  120.         if (noBomFile(fileName)) {  
  121.             System.out.println("skip :" + fileName);  
  122.             return;  
  123.         }  
  124.           
  125.           
  126.           
  127.         FileInputStream fin = new FileInputStream(fileName);  
  128.         // 开始写临时文件  
  129.         InputStream in = getInputStream(fin);  
  130.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  131.         byte b[] = new byte[4096];  
  132.   
  133.         int len = 0;  
  134.         while (in.available() > 0) {  
  135.             len = in.read(b, 04096);  
  136.             // out.write(b, 0, len);  
  137.             bos.write(b, 0, len);  
  138.         }  
  139.   
  140.         in.close();  
  141.         fin.close();  
  142.         bos.close();  
  143.   
  144.         // 临时文件写完,开始将临时文件写回本文件。  
  145.         System.out.println("[" + fileName + "]");  
  146.         FileOutputStream out = new FileOutputStream(fileName);  
  147.         out.write(bos.toByteArray());  
  148.         out.close();  
  149.         System.out.println("PROCESS FILE " + fileName);  
  150.     }  
  151.   
  152.     // check the file is or not bom file  
  153.     private static boolean noBomFile(String  fname) throws IOException {  
  154.         FileInputStream fin = new FileInputStream(fname);  
  155.         PushbackInputStream testin = new PushbackInputStream(fin);  
  156.         int ch = testin.read();  
  157.         int ch2 = testin.read();  
  158.         int ch3 = testin.read();  
  159.   
  160.         if (ch == 0xEF && ch2 == 0xBB && ch3 == 0xBF) {  
  161.            
  162.             return false// is bom file  
  163.         } else {  
  164.            
  165.             return true;  
  166.         }  
  167.     }  
  168.   
  169.     /** 
  170.      * 根据ant编译错误来去除bom 
  171.      *  
  172.      * @param resultFile 
  173.      * @throws IOException 
  174.      */  
  175.     // public static void trimBomByCompileResult(String resultFile)  
  176.     // throws IOException {  
  177.     // Set set = getFileNamesFromCompileResult(resultFile);  
  178.     //  
  179.     // for (Iterator it = set.iterator(); it.hasNext();) {  
  180.     // String fName = it.next().toString();  
  181.     // trimBom(fName);  
  182.     // }  
  183.     //  
  184.     // }  
  185.   
  186.     public static void main(String[] args) throws IOException {  
  187.         // if(args.length==0){  
  188.         // DealSrcFiles(System.getProperty("user.dir"));  
  189.         // }  
  190.         // else{  
  191.         // DealSrcFiles(args[0]);  
  192.         // }  
  193.         String path = "D://Documents and Settings//Administrator//桌面//0301//de bom xml";  
  194.         DealSrcFiles(path);  
  195.     }  
  196. }  
import java.io.BufferedReader;import java.io.ByteArrayOutputStream;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.Iterator;import java.util.List;import java.util.Set;public class BOMRemover { /**  * ant 编译之后的result文件,注意要编译提示错误的文件名要在同一行 可以设置命令提示窗口的缓冲区大小实现  *   * @param resultFileName  */ public static Set getFileNamesFromCompileResult(String resultFileName)   throws java.io.IOException {  Set 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; }  //trim dir public static void DealSrcFiles(String path) {  if (path.charAt(path.length() - 1) != '//') {   path += '//';  }  File file = new File(path);  if (!file.exists()) {   System.out.println("Error: Path not Existed! Please Check it out!");   return;  }  String[] filelist = file.list();  for (int i = 0; i < filelist.length; i++) {   File temp = new File(path + filelist[i]);   if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {    DealSrcFiles(path + filelist[i]);   } else {    if (filelist[i].endsWith(".xml")) {     try {      // System.out.println(path + filelist[i]);      trimBom(path + filelist[i]);     } catch (Exception eee) {      System.out.println(eee.getMessage());     }    }   }  } } /**  * 读取流中前面的字符,看是否有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) { // if ch==0xef   testin.unread(ch);   testin.unread(0xef);  } else if ((ch = testin.read()) != 0xBF) { // if ch ==0xbb   throw new IOException("错误的UTF-8格式文件");  } else { // if ch ==0xbf   // 不需要做,这里是bom头被读完了   // // System.out.println("still exist bom");  }  return testin; } /**  * 根据一个文件名,读取完文件,干掉bom头。  *   * @param fileName  * @throws IOException  */ public static void trimBom(String fileName) throws IOException {   if (noBomFile(fileName)) {   System.out.println("skip :" + fileName);   return;  }        FileInputStream fin = new FileInputStream(fileName);  // 开始写临时文件  InputStream in = getInputStream(fin);  ByteArrayOutputStream bos = new ByteArrayOutputStream();  byte b[] = new byte[4096];  int len = 0;  while (in.available() > 0) {   len = in.read(b, 0, 4096);   // out.write(b, 0, len);   bos.write(b, 0, len);  }  in.close();  fin.close();  bos.close();  // 临时文件写完,开始将临时文件写回本文件。   System.out.println("[" + fileName + "]");  FileOutputStream out = new FileOutputStream(fileName);  out.write(bos.toByteArray());  out.close();  System.out.println("PROCESS FILE " + fileName); } // check the file is or not bom file private static boolean noBomFile(String  fname) throws IOException {  FileInputStream fin = new FileInputStream(fname);  PushbackInputStream testin = new PushbackInputStream(fin);  int ch = testin.read();  int ch2 = testin.read();  int ch3 = testin.read();  if (ch == 0xEF && ch2 == 0xBB && ch3 == 0xBF) {      return false; // is bom file  } else {      return true;  } } /**  * 根据ant编译错误来去除bom  *   * @param resultFile  * @throws IOException  */ // public static void trimBomByCompileResult(String resultFile) // throws IOException { // Set set = getFileNamesFromCompileResult(resultFile); // // for (Iterator it = set.iterator(); it.hasNext();) { // String fName = it.next().toString(); // trimBom(fName); // } // // } public static void main(String[] args) throws IOException {  // if(args.length==0){  // DealSrcFiles(System.getProperty("user.dir"));  // }  // else{  // DealSrcFiles(args[0]);  // }  String path = "D://Documents and Settings//Administrator//桌面//0301//de bom xml";  DealSrcFiles(path); }}

 

 

 

原工具来源于 http://hi.baidu.com/joyfun/blog/item/ca926f06c40cc0710308815b.html

去除java代码中的UTF-8 BOM

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值