The garbled problem in Java
Recently do projects often encounter Java garbled problem, so the time to tidy up the situation and how to deal with garbled problem, here do a collation,
Analysis
Coding and decoding
Encoding is the conversion of characters into bytes, and decoding is the translation of bytes into characters.
byte stream and character streams
The reading and writing of files is done through a byte stream, even though there is a stream of characters in Java, but the byte streams are still used at the bottom.
Garbled problem appears
The most frequently used in Java is the character, which needs to be decoded when we read the file into memory and display it in the console (the byte stream---> character streams). If the file is UTF-8 encoding, and we decode the error into GBK (if you do not specify the encoding, Java will take the system default encoding) to decode, then can only display garbled. When we write a file, it's best to specify the encoding (UTF-8).
Solution
Example 1
When you convert a stream of bytes into character streams, we specify the encoding format. This is our file, and it should be gb2312 code.
public static String Read (String filename) throws Exception {
InputStream is = new FileInputStream (filename);
BufferedReader in = new BufferedReader (The new InputStreamReader (IS,
"gb2312")); Specifies the encoding format
String s;
StringBuilder sb = new StringBuilder ();
while ((s = in.readline ())!= null) {
Sb.append (s + "\ n")
;
In.close ();
return sb.tostring ();
}
Example 2
Read directly through a byte stream, specifying the encoding when converting to characters using string.
Package Com.dy.xidian;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Class Bufferedinputfile {public
static string read (String filename) throws Exception {
@SuppressWarnings (" Resource ")
InputStream is = new FileInputStream (filename);
Byte[] B = new byte[1024];
Is.read (b);
return new String (b, "gb2312");
}
public class Memoryinput {public
static void Main (string[] args) throws Exception {
String filename = ' e:/html/ Gb2312.php ";
String s = bufferedinputfile.read (filename);
System.out.println (s);
}
Trap
There is a FileReader class in I/O operations that hides the details of the byte stream being converted to character streams, which we can use. BufferedReader in = new BufferedReader (new FileReader (filename)); In this way, we get the character stream directly. But we found that we did not set the encoding, because the default encoding is used in FileReader. This becomes very dangerous, if its default encoding format and our file encoding is different, then read out the data must be garbled. So we'd better use the way in the example to convert the stream.
Thank you for reading, I hope to help you, thank you for your support for this site!