Java obtains the txt file encoding format

Source: Internet
Author: User

We encountered a time-consuming task in our work. To read txt documents without garbled characters, We need to encode and convert the txt documents. Open the txt file and save it as the required encoding format. If the txt file is small, the conversion is not too slow, but if the file is too large, the program will be suspended when it is opened, which is inconvenient. So I was wondering if I could use a program to write a conversion tool. If I don't need to open the document, I can click OK to automatically convert it? So I wrote a program in c.

Now I started to do java, so I am bored. I just want to use java to do a similar program?

First, the encoding format in java is studied. Found in java

 

Java encoding corresponds to txt Encoding

Java

Txt

Unicode

Unicode big endian

UTF-8

UTF-8

UTF-16

Unicode

Gb2312

ANSI

Java reads txt files. If the encoding format does not match, garbled characters may occur. Therefore, you must set the read encoding when reading a txt file. The txt file encoding format is written in the file header. in the program, you must first parse the file encoding format. After obtaining the encoding format, no garbled code will be generated when you read the file in this format.

  1. InputStream inputStream =NewFileInputStream ("E:/1.txt ");
  2. Byte[] Head =New Byte[3];
  3. InputStream. read (head );
  4. String code = "";
  5. Code = "gb2312 ";
  6. If(Head [0] =-1 & head [1] =-2)
  7. Code = "UTF-16 ";
  8. If(Head [0] =-2 & head [1] =-1)
  9. Code = "Unicode ";
  10. If(Head [0] =-17 & head [1] =-69 & head [2] =-65)
  11. Code = "UTF-8 ";
  12. System. out. println (code );

In this way, the txt encoding format is obtained.

This article is from TinyKing"
 

Related Article

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.