Https://code.google.com/p/juniversalchardet/downloads/list
Java porting Mozilla's Code Auto-detection library (source code for C + +), accurate rate is high.
Check out the read-only version of the code through SVN:
# Non-members may check out a read-only working copy anonymously over HTTP.
SVN checkout http://juniversalchardet.googlecode.com/svn/trunk/juniversalchardet-read-only
PackageMyjava;ImportJava.io.File;Importjava.io.IOException;ImportOrg.mozilla.universalchardet.UniversalDetector; Public classTestdetector { Public Static voidMain (string[] args)throwsjava.io.IOException {String folder= "/home/hadoop/test/charset/"; File File=NewFile (folder); for(File _file:file.listfiles ()) Detectcharset (_file.getabsolutepath ()); } Static voidDetectcharset (String fileName)throwsIOException {byte[] buf =New byte[4096]; Java.io.FileInputStream FIS=NewJava.io.FileInputStream (fileName); //(1)Universaldetector detector =NewUniversaldetector (NULL); //(2) intnread; while((Nread = Fis.read (buf)) > 0 &&!Detector.isdone ()) {Detector.handledata (buf,0, nread); } //(3)Detector.dataend (); //(4)String encoding =Detector.getdetectedcharset (); if(Encoding! =NULL) {System.out.println ("Detected encoding =" +encoding); } Else{System.out.println ("No encoding detected."); } //(5)Detector.reset (); }}
Can be combined with another Java character Set detection library to ensure better results, because for the short passage, the above detection method may not be able to draw conclusions.
At the same time, because this algorithm comes from Mozilla, it should be better for the detection of HTML and other label files.
Http://cpdetector.sourceforge.net/usage.shtml
Java CharSet Detector