默认使用的就是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
原文地址:https://www.cnblogs.com/-nbloser/p/8978065.html
时间: 2024-11-11 17:18:14