Solve the garbled problem of byte stream and character stream (conversion stream)

The root cause of garbled

Encoding and decoding rules are different

For example: IDEA reads files by default, the encoding format is UTF-8, and the default storage format for windows system files is GBK. When IDEA reads files under windows, if no special processing is performed, garbled characters may appear.

Conversion flow

  • InputStreamReader
  • OutputStreamWriter

Read file

You can use the conversion stream InputStreamReaderto solve the garbled problem.
IDEA default encoding is UTF-8, and read GBK files.
Insert picture description here
Use the conversion stream to set the read encoding as GBK
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");

@Test
public void Test3() throws IOException {
    
    

	  InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");
	   int read;
	   while((read=isr.read())!=-1){
    
    
	       System.out.println((char) read);
	       System.out.println(read);
	   }
	   isr.close();
}

输出结果:
你好
abc
哈哈哈

Write file

@Test
public void Test6() throws IOException {
    
    

    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("b.txt"),"GBK");
    osw.write("你好");
    osw.write("\r\n");
    osw.write("abc");
    osw.write("\r\n");
    osw.flush();
    osw.write("哈哈哈");
    osw.close();
}

Result: The encoding format is GBK


Insert picture description here



InputStreamReader类

Conversion stream java.io.InputStreamReader, a subclass of Reader, is a bridge from byte stream to character stream. It reads bytes and decodes them into characters using the specified character set. Its character set can be specified by the name, or it can accept the platform's default character set.

Construction method

  • InputStreamReader(InputStream in): Create a character stream using the default character set.
  • InputStreamReader(InputStream in, String charsetName): Create a character stream with a specified character set.

OutputStreamWriter类

Conversion stream java.io.OutputStreamWriter, a subclass of Writer, is a bridge from character stream to byte stream. Use the specified character set to encode characters into bytes. Its character set can be specified by the name, or it can accept the platform's default character set.

Construction method

  • OutputStreamWriter(OutputStream in): Create a character stream using the default character set.
  • OutputStreamWriter(OutputStream in, String charsetName): Create a character stream with a specified character set.

Guess you like

Origin blog.csdn.net/ren9436/article/details/108165249