021.4 IO流——字节、字符桥梁(编码解码)
程序员文章站
2022-05-03 22:57:56
默认使用的就是gbk编码,这里的例子改成了utf8编码写入—编码 读取—解码 字符流 = 字节流 + 编码表#####################快捷操作的类FileWriter and FileReader ......
默认使用的就是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