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";