编码转换流
- 专门用来把 字节流 转成 字符流。是一个字节和字符流的转换桥梁。
- InputStreamReader – 把 字节读取流转成 字符读取流,继承自Reader。
- 创建对象
- InputStreamReader(InputStream in)
- InputStreamReader(InputStream in, String charsetName)
- 创建对象
- OutputStreamWriter – 把 字节写出流转成 字符写出流,继承自Writer。
- 创建对象
- OutputStreamWriter(OutputStream out)
- OutputStreamWriter(OutputStream out, String charsetName)
- 创建对象
/**
* 用来把字节流转换成字符流
* InputStreamReader -- 把字节读取流转成字符读取流,继承自Reader。
* OutputStreamWriter -- 把字节写出流转成 字符写出流,继承自Writer。
*/
public class 编码转换流 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\qwer\\qwer.txt");
File file02 = new File("D:\\qwer\\qwer02.txt");
out(file); //测试 OutputStreamWriter
in(file02); //测试 InputStreamReader
}
private static void in(File file02) throws Exception {
Reader r =new InputStreamReader(new FileInputStream(file02),"gbk");
int b;
while ((b=r.read())!=-1){
System.out.println(b);
}
r.close();
}
private static void out(File file) throws Exception {
Writer w = new OutputStreamWriter(new FileOutputStream(file),"gbk");
w.write(25105);
w.write(29233);
w.write(20320);
w.close();
}
}
执行结果:
第一个函数的结果:
第一个函数的结果: