021.4 IO stream - byte, character bridge (encoding and decoding)

The default is gbk encoding, the example here is changed to utf8 encoding

write-encoding

private static void writeText() throws IOException
{
    FileOutputStream fos = new FileOutputStream("utf8.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
    osw.write("求");
    osw.close ();
}

read-decode

private static void readCNText() throws IOException
{
    FileInputStream fis = new FileInputStream("utf8.txt");
    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    int i = 0;
    while((i = isr.read())!=-1){
        System.out.println((char)i);
    }
    isr.close();
}

 

Character stream = byte stream + encoding table


##################### Shortcut class
FileWriter and FileReader

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325107624&siteId=291194637