021.4 IO流——字节、字符桥梁(编码解码)

默认使用的就是gbk编码,这里的例子改成了utf8编码

写入—编码

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

读取—解码

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();
}

 

字符流 = 字节流 + 编码表


#####################快捷操作的类
FileWriter  and   FileReader

posted @ 2018-05-01 23:29 _nbloser 阅读(...) 评论(...) 编辑 收藏