S(1)文本文件的编码方式识别

java识别文本文件的编码方式应用很广,并且进行检测识别的开源项目也很多,这里说的是开源juniversalchardet,juniversalchardet是用的mozilla的universalchardet库。

简单的测试用例(案例来自于juniversalchardet官方网站,juniversalchardet jar文件下载官方网站http://code.google.com/p/juniversalchardet/)

import org.mozilla.universalchardet.UniversalDetector; 
public class TestDetector {   
public static void main(String[] args) throws java.io.IOException {    
    byte[] buf = new byte[4096];     
    String fileName = args[0];     
    java.io.FileInputStream fis = new java.io.FileInputStream(fileName);       
    UniversalDetector detector = new UniversalDetector(null);     
    int nread;    
    while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {      
        detector.handleData(buf, 0, nread);     
    }  
    detector.dataEnd();     
    String encoding = detector.getDetectedCharset();     
    if (encoding != null) {     
         System.out.println("Detected encoding = " + encoding);    
     } else {      
         System.out.println("No encoding detected.");    
     }    
    }
 }

(仅给大家推荐一个识别文件编码方式的方法) 

















你可能感兴趣的:(java,编码格式,编码,编码方式)