Java CharSet Detector

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.