Garbage code problem in java
Recently, when doing projects, I often encounter garbled code problems in Java, so I took the time to sort out the garbled code problems and how to deal with them. Here I have compiled them.
analyze
Encoding and decoding
Encoding is to convert characters into bytes, and decoding is to convert bytes into characters.
Byte Stream and Character Stream
Reading and writing files are all implemented through byte streams. Even if there is a character stream in JAVA, the underlying layer still uses the byte stream.
Garbage code problem occurs
The most frequently used characters in Java are characters. When we read the file into memory and display it in the console (byte stream--->character stream), we need to use decoding. If the file is UTF-8 encoding and we misuse it to GBK when decoding (if the encoding is not specified, JAVA will adopt the system default encoding) to decode it, then only garbled code can be displayed. When we write files, it is best to specify the encoding (UTF-8).
Solution
Example 1
When converting a byte stream to a character stream, we specify the encoding format. This is our file should also be encoded by gb2312
public static String read(String filename) throws Exception { InputStream is = new FileInputStream(filename); BufferedReader in = new BufferedReader(new InputStreamReader(is, "gb2312")); //Specify 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 in directly through the byte stream, and specify the encoding when converting it 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. This class hides the details of byte streaming into character streams, which we can use in this way. BufferedReader in = new BufferedReader(new FileReader(filename)); In this way, we directly get the character stream. But we found that we did not set the encoding, because the default encoding method is adopted in FileReader. This becomes very dangerous. If its default encoding format is different from the encoding of our file, then the read data must be garbled. So we'd better use the method in the example to convert the stream.
Thank you for reading, I hope it can help you. Thank you for your support for this site!