Java IO stream - detailed introduction to the use of conversion stream

conversion stream

Previously, our code encoding and file encoding were both UTF-8, so there was no problem of Chinese garbled characters

We know that if the format of the code encoding and the file encoding are inconsistent, there will be problems with Chinese garbled characters

So if in development, we do encounter coding inconsistencies, how to solve it ?

We can transform the stream using character input

The raw byte stream of the file (GBK) can be extracted and there will be no problem with the raw bytes.

Then convert the byte stream into a character input stream with the specified encoding, so that the characters in the character input stream are not garbled

insert image description here

character input conversion stream

Character input conversion stream: implement the class InputStreamReader, which can convert the original byte stream into a character input stream according to the specified encoding

Commonly used constructors for character input conversion streams are as follows ( the second constructor is commonly used, and the first constructor is hardly used ):

constructor illustrate
InputStreamReader(InputStream is) The original byte stream can be converted into a character input stream according to the default encoding of the code. ( Barely used, same as the default FileReader )
InputStreamReader(InputStream is ,String charset) The original byte stream can be converted into a character input stream according to the specified encoding, so that the characters in the character stream will not be garbled ( emphasis )

For example, our file encoding is "GBK", and the code encoding is UTF-8

public static void main(String[] args) throws Exception {
    
    
    // 代码UTF-8 文件GBK
    // 1. 提取GBK文件的原始字节流
    InputStream is = new FileInputStream("/Users/chenyq/Documents/test2.txt");

    // 2. 字符输入转换流: 把原始字节流转为字符输入流(以GBK编码的方式将文件转为字符输入流)
    Reader isr = new InputStreamReader(is, "GBK");

    // 3. 把转换的字符输入流再转换为缓冲输入流
    BufferedReader br = new BufferedReader(isr);

    // 读取文件内容
    String line;
    while ((line = br.readLine()) != null) {
    
    
        System.out.println(line);
    }
}

character output conversion stream

What if we need to control the encoding used to write out the characters ?

Method 1: Characters can be obtained by specifying the encoding and then written out using the byte output stream ( this method is described in the previous article, Character Set Encoding and Decoding )

  • “我爱你中国”.getBytes(编码)

Method 2: It can be realized by using the character output conversion stream.

Character output conversion stream :

Character input conversion stream: OutputStreamWriter, which can convert the byte output stream into a character output stream according to the specified encoding.

The commonly used constructors for character output conversion streams are as follows ( the second constructor is commonly used, and the first constructor is hardly used ):

constructor illustrate
OutputStreamWriter(OutputStream os) The original byte output stream can be converted into a character output stream according to the default encoding of the code. Hardly used.
OutputStreamWriter(OutputStream os,String charset) The original byte output stream can be converted into a character output stream according to the specified encoding (emphasis)
public static void main(String[] args) throws Exception {
    
    
    // 1. 定义一个原始字节输出流
    OutputStream os = new FileOutputStream("/Users/chenyq/Documents/test2.txt");

    // 2. 字符转换输出流: 将原始的字节输出流转为字符输出流(指定GBK编码的方式写字符出去)
    Writer osw = new OutputStreamWriter(os, "GBK");

    // 3. 将转换的原始字符输出流载转换为缓冲字符输出流
    Writer bw = new BufferedWriter(osw);

    bw.write("我爱中国");
    bw.write("我爱学习");
    bw.write("我爱Java");

    bw.close();
}

Guess you like

Origin blog.csdn.net/m0_71485750/article/details/127604832