如何获得常用文本文件的编码

     学文本处理,对文本的乱码问题是很头疼的。

     今天弄Lucene的时候,又遇到了这个问题,我就想,能不能预先获得文件的编码方式呢?

事实证明是可以的。

     这里涉及到文件头信息的问题,至于其中的原理,我在网上搜索了一下,也没有弄明白。

只是找到的段代码:

 
 
  1. package com.xh; 
  2.  
  3. import java.io.BufferedInputStream; 
  4. import java.io.BufferedReader; 
  5. import java.io.FileInputStream; 
  6. import java.io.InputStreamReader; 
  7.  
  8. public class DetectingFileCoding { 
  9.     /** 
  10.      * 判断文件的编码格式 
  11.      * @param fileName :file 
  12.      * @return 文件编码格式 
  13.      * @throws Exception 
  14.      */ 
  15.     public static String codeString(String fileName) throws Exception{ 
  16.         BufferedInputStream bin = new BufferedInputStream( 
  17.         new FileInputStream(fileName)); 
  18.         int p = (bin.read() << 8) + bin.read(); 
  19.         String code = null
  20.          
  21.         switch (p) { 
  22.             case 0xefbb
  23.                 code = "UTF-8"
  24.                 break
  25.             case 0xfffe
  26.                 code = "Unicode"
  27.                 break
  28.             case 0xfeff
  29.                 code = "UTF-16BE"
  30.                 break
  31.             default
  32.                 code = "GBK"
  33.         } 
  34.          
  35.         return code; 
  36.     } 
  37.     public String  getContent(String file) { 
  38.         try { 
  39.             FileInputStream fInputStream = new FileInputStream(file); 
  40.             //code为上面方法里返回的编码方式 
  41.             InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, codeString(file)); 
  42.             BufferedReader in = new BufferedReader(inputStreamReader); 
  43.             StringBuffer sBuffer=new StringBuffer(); 
  44.             String strTmp = ""
  45.             //按行读取 
  46.             while (( strTmp = in.readLine()) != null) { 
  47.                 sBuffer.append(strTmp + "/n"); 
  48.             } 
  49.             in.close(); 
  50.             inputStreamReader.close(); 
  51.             fInputStream.close(); 
  52.             return sBuffer.toString(); 
  53.         } catch (Exception e) { 
  54.             // TODO: handle exception 
  55.             e.printStackTrace(); 
  56.             return null
  57.         } 
  58.          
  59.     } 
  60.     public static void main(String[] args) { 
  61.         DetectingFileCoding DFC=new DetectingFileCoding(); 
  62.         System.out.println(DFC.getContent("gbkfile.txt")); 
  63.         System.out.println(DFC.getContent("utf-8file.txt")); 
  64.         System.out.println(DFC.getContent("unicodefile.txt")); 
  65.     } 

我自己创建了三个txt文件,都放到Project的根目录下,截图如下:

程序运行结果如下:

不管怎么样,这也不失为一种处理文件的方法:

源程序来自于:http://blog.csdn.net/paul630/article/details/6164390

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值