InputStreamReader(字符输入转换流 )
- 解决不同编码时,字符流读取文本内容乱码的问题
public static void main(String[] args) {
try (
//1.得到文件的原始字节流(GBK的字节流形式)
FileInputStream is = new FileInputStream("src/666.txt");
//2.把原始的字节流输入按照指定的字符集编码转换成字符输入流
InputStreamReader isr = new InputStreamReader(is, "GBK");
//3.再转换成缓冲流
BufferedReader br = new BufferedReader(isr)
) {
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
OutputStreamWriter(字符输出转换流 )
public static void main(String[] args) {
try (
FileOutputStream os = new FileOutputStream("src/666.txt");
OutputStreamWriter osw = new OutputStreamWriter(os,"GBK");
BufferedWriter bw = new BufferedWriter(osw)
) {
bw.write("我是你爹");
bw.newLine();
bw.write("我是你爹");
} catch (Exception e) {
e.printStackTrace();
}
}