How to determine the file encoding that Java.io.File reads

Source: Internet
Author: User

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
    1. Googlecode
    2. GitHub

How to determine the file encoding that Java.io.File reads

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.