JAVA single row diary -2020 / 1 / 29- commutations

1. The character set and character encoding

  • Character Encoding: a natural language character binary number corresponding to the rules of
    encoding: Character (can understand) -> bytes (read)
    decoding: bytes (read) -> character (can read)
  • The character set (code table)

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

2. Conversion flow

  • (E.g. garbled when using UTF-8 with the software reads the Windows file system default encoding GBK) conversion between different code sets will be garbled, the stream can be specified using the conversion code table: reasons for usingRead any file encoding format
  • Switching principle:
    Here Insert Picture Description

2.1OutputStreamWriter

  • OutputStreamWriterIt is Writersubclass
  • Construction method
OutputStreamWtiter(OutputStream out) 使用默认字符编码
OutputStreamWtiter(OutputStream out,String charsetName) 使用指定的字符编码
  • OutputStream out: Byte output stream, can be used to write the write destination
  • String charsetName: Encoding table name, case insensitive, UTF-8 / utf-8 (default), GBK / gbk ...
  • Steps for usage
  1. Creating OutputStreamWriter()the class object, method of construction and the specified output stream fill character code table
  2. Using the write()method, converts the character bytes stored inBuffer
  3. Using the flush()method, will refresh the data buffer to a file
  4. Release resources
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Demmo01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream file = new FileOutputStream("G:\\Java\\测试文件夹\\GBK.txt");
        OutputStreamWriter Osw = new OutputStreamWriter(file,"GBK");

        char[] chars = {'a','b','你','好'};
        Osw.write(chars);
        Osw.flush();

        Osw.close();
    }
}

Here Insert Picture Description

2.2InputStreamReader

  • InputStreamReaderIt is Readersubclass
  • Construction method
InputStreamReader(InputStream in) 使用默认字符编码
InputStreamReader(InputStream in,String charsetName) 使用指定的字符编码
  • Steps for usage
  1. Create a InputStreamReader()class object, constructor and fill in the character input stream designated coding table
  2. Using the read()method, data is read into memory according to a specified code table
  3. Release resources
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        InputStreamReader in = new InputStreamReader(new FileInputStream("G:\\Java\\测试文件夹\\GBK.txt"), "GBK");
        int len = 0;
        while ((len = in.read()) != -1) {
            System.out.println((char) len);
        }
        in.close();
    }
}

Here Insert Picture Description
Using UTF-8:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        InputStreamReader in = new InputStreamReader(new FileInputStream("G:\\Java\\测试文件夹\\GBK.txt"), "UTF-8");
        int len = 0;
        while ((len = in.read()) != -1) {
            System.out.println((char) len);
        }
        in.close();
    }
}

Here Insert Picture Description

Published 105 original articles · won praise 1 · views 3391

Guess you like

Origin blog.csdn.net/wangzilong1995/article/details/104104604