在io中分为字节流和字符流,但有时两者之间也可能需要转换,Java中为我们提供了InputStreamReader和OutoutStreamWtiter使两者之间转化。他们一个是reader的子类一个是writer的子类。
package com.package18;
import java.io.*;
public class Examples {
public static void main(String[] args)throws IOException {
//1.将字节输入流转换成字符输入流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("./out.txt")));
//2.将字节输出流转换成字符输入流
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./text/file.txt")));
int tempp=0;
while ((tempp=bufferedReader.read())!=-1){
bufferedWriter.write(tempp);
}
//2.将字节输出流转换成字符输入流
BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./text/file.txt",true)));
//1.将字节输出流转换为字符输出流
String str="我是中国人 也是河南人";
bufferedWriter2.write(str);
bufferedReader.close();
bufferedWriter.flush();
bufferedWriter.close();
bufferedWriter2.flush();
bufferedWriter2.close();
}
}