第三方jar包解读文件编码

Code:
  1. package test;  
  2.   
  3. //import cpdetector.io.*;   
  4. import info.monitorenter.cpdetector.io.CodepageDetectorProxy;  
  5. import info.monitorenter.cpdetector.io.JChardetFacade;  
  6.   
  7. import java.io.*;  
  8. import java.nio.charset.*;  
  9.   
  10. public class PageCode {  
  11.     public static void main(String args[]) {  
  12.   
  13.         CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();  
  14.         detector.add(JChardetFacade.getInstance());  
  15.   
  16.         Charset charset = null;  
  17.   
  18.         File f = new File("D:/test/1.xls");  
  19.   
  20.         try {  
  21.             charset = detector.detectCodepage(f.toURL());  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         if (charset != null) {  
  26.             System.out.println(f.getName() + "编码是:" + charset.name());  
  27.         } else  
  28.             System.out.println(f.getName() + "未知");  
  29.     }  
  30. }  

原理:

Code:
  1. import java.io.*;    
  2.     
  3. public class TestText {    
  4.     public static void main(String args[]){    
  5.         File file = new File("C:/1.txt");    
  6.             
  7.         try{    
  8.             InputStream stream = new FileInputStream(file);    
  9.             byte[] byteArray = new byte[3];    
  10.                 
  11.             stream.read(byteArray);    
  12.             stream.close();    
  13.                 
  14.             if (byteArray[0] == -17 && byteArray[1] ==  -69 && byteArray[2] == -65)    
  15.                 System.out.println("UTF-8");    
  16.             else    
  17.                 System.out.println("可能是其他的编码");    
  18.                 
  19.         }    
  20.         catch(Exception e){    
  21.             e.printStackTrace();    
  22.         }    
  23.     }    
  24. }   





http://blog.csdn.net/justinavril/archive/2008/08/07/2783266.aspx

jar包下载地址:
http://ncu.dl.sourceforge.net/project/cpdetector/cpdetector/binaries/cpdetector_1.0.7_binary.zip

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值