Java处理文件BOM头的方式推荐

背景:

java普通的文件读取方式对于bom是无法正常识别的。

   使用普通的InputStreamReader,如果采用的编码正确,那么可以获得正确的字符,但bom仍然附带在结果中,很容易导致数据处理出错。
另外,对于存在BOM头的文件,无法猜测它使用的编码。

目标:

实现一种方式,可对BOM头进行捕捉和过滤


解决方案有二:

一、  使用apache的工具类,以BOMStream为例:

    BOMStream,api参考:http://commons.apache.org/io/apidocs/org/apache/commons/io/input/BOMInputStream.html

   该类的构造方式:
   BOMInputStream bomIn = new BOMInputStream(in) //仅能检测到UTF8的bom,且在流中exclude掉bom
   
   BOMInputStream bomIn = new BOMInputStream(in, include); //同上,且指定是否包含

   也可以指定检测多种编码的bom,但目前仅支持UTF-8/UTF-16LE/UTF-16BE三种,对于UTF32之类不支持。
   BOMInputStream bomIn = new BOMInputStream(in, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE);
   
   有用的方法:
   bomIn.hasBOM()、hasBOM(ByteOrderMask.**)可用于判断当前流中是否检测到了bom。



   读取文件示例:

 FileInputStream fis = new FileInputStream(file);
   //可检测多种类型,并剔除bom
   BOMInputStream bomIn = new BOMInputStream(in, false,ByteOrderMark.UTF-8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE);
   String charset = "utf-8";
   //若检测到bom,则使用bom对应的编码
   if(bomIn.hasBOM()){
      charset = bomIn.bs.getBOMCharsetName();
   }
   InputStreamReader reader = new InputStreamReader(bomIn, charset);
   ...

 

二、使用一个更强大点的工具类(可以支持UTF-8/UTF-16LE/UTF-16BE/UTF-32LE/UTF-32BE):
       

   参考地址:http://koti.mbnet.fi/akini/java/unicodereader/, 下载其中两个文件:UnicodeStream和UnicodeReader

   以UnicodeReader为例:

   FileInputStream fis = new FileInputStream(file);
   UnicodeReader ur = new UnicodeReader(fis, "utf-8");
   BufferedReader br = new BufferedReader(ur);
   ...

 

   相较于Apache的工具类,这里的UnicodeReader 支持更多的BOM编码。

   源码解读:

   UnicodeReader 通过PushbackInputStream+InputStreamReader实现BOM的自动检测和过滤读取;

   当没有检测到BOM时,pushback流将回退,并采用构造函数传入的编码进行读取。
   否则使用BOM对应的编码进行读取。

   

  相对来说,第二种方式更加轻量和强大;另外也更加透明,可以随便修改源码来实现自己的需求。

 

原文地址:http://blog.csdn.net/littleatp2008/article/details/6943215

posted @ 2011-11-07 14:15  美码师  阅读(5568)  评论(0编辑  收藏  举报