是FileReader的父类
InputStreamReader读InputStream送过来的字节时,会查询idea默认码表UTF-8,也可以查询指定的编码表,进行解码。
构造方法:
package convertedStream;
import java.io.*;
public class converted {
public static void main(String[] args) throws IOException {
//1.创建转换流,绑定输入文件,并指定编码格式
InputStreamReader inre = new InputStreamReader(new FileInputStream("b.txt"),"gbk");
//2.读入内存中
char[] chars = new char[1024];
int len = 0;
while((len =inre.read(chars)) != -1){
System.out.println(chars);
}
//3.释放资源
inre.close();
}
}
package convertedStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class converted {
public static void main(String[] args) throws IOException {
//1.创建转换流,绑定输出对象
OutputStreamWriter otw = new OutputStreamWriter(new FileOutputStream("b.txt"),"gbk");
//2.默认会以utf-8写入,OutputStreamWriter - FileOutputStream - 写入
otw.write("你好呀");
//3.释放资源
otw.close();
}
}
原文:https://www.cnblogs.com/huxiaobai/p/11609145.html