How to determine the file encoding that Java.io.File readsProblem
When it comes to file reads in Java, file encoding is often a concern. Although the program generally specifies UTF-8 encoding, but the user may always submit a variety of encoded files (especially under Windows users), if the files are not judged directly according to the way UTF-8 read, is bound to garbled.
Solution Solutions
Java native does not support the decision of file encoding, generally read the first few bytes of the file to judge, the need to write their own tool class, the number of types of judgement is also relatively small. Recently found an open source project juniversalchardet
, can be more elegant to complete the task.
The use of the method is also very simple, download its jar package, according to the example of the official website can be, of course, this piece of code itself encapsulated into a tool class is the best:
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); // (1) UniversalDetector detector = new UniversalDetector(null); // (2) int nread; 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(); }}
When I wrote the tool test myself, I found that some files could not be judged (such as Excel saved GBK encoded. csv file), so use it to determine the file encoding, it is best to deal with the situation under the code, such as to a default encoding what.
Juniversalchardet's Project Address
- Googlecode
- GitHub
How to determine the file encoding that Java.io.File reads