InputStreamReader与OutputStreamWriter都能够通过其构造方法将字节流转为字符流,不同点在于流向不同:
InputStreamReader继承自Reader其构造方法为:
public static void test()throws Exception{
File file = new File("e:/data.txt");
FileInputStream inputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
for(int i = 0;i < file.length();i++){
System.out.print((char)inputStreamReader.read());
}
}
OutputStreamWriter继承自Writer其构造方法为:
public static void test()throws Exception{
File file = new File("e:/data.txt");
OutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"utf-8");
System.out.println(outputStreamWriter.getEncoding());
outputStreamWriter.write("abcdefg",0,5);
outputStreamWriter.flush();
outputStreamWriter.close();
}