"Java io Knowledge" read Chinese garbled Problem solving

Source: Internet
Author: User

Read the file is mainly to set the read input stream, such as InputStreamReader to set the decoding format read, and the file itself is the same encoding format, so that the read out will not garbled.

InputStreamReader reader = new Inputstreaminputreader (input, "encoding format");

ANSI is "GBK"

Note: You must use the correct decoding format when reading, otherwise it will be garbled ....

And to convert the encoding format only after the correct read to turn, otherwise the conversion will not be successful or garbled.

How to change the format:

Str.getbyte[] ("coded format");

New String (str.getbyte[] (), "format to convert")

Java Read txt file, if the encoding format does not match, there will be garbled phenomenon, so read txt files need to set the reading code. TXT document encoding format is written in the file header, in the program needs to first parse the file encoding format, get the encoding format, in this format to read files will not produce garbled. InputStream InputStream =NewFileInputStream ("E:/1.txt");byte[] head =New byte[3];            Inputstream.read (head);                   String code = ""; Code = "gb2312";if(Head[0] = =-1 && head[1] = = 2) code = "UTF-16";if(Head[0] = =-2 && head[1] = = 1) code = "Unicode";if(head[0]==-17 && head[1]==-69 && head[2] ==-65)                code =  "UTF-8"; 

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.