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.
- 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 ";
- System. out. println (code );
In this way, the txt encoding format is obtained.
This article is from TinyKing"