在io中分为字节流和字符流,但有时两者之间也可能需要转换,Java中为我们提供了InputStreamReader和OutoutStreamWtiter使两者之间转化。他们一个是reader的子类一个是writer的子类。

    1. package com.package18;
    2. import java.io.*;
    3. public class Examples {
    4. public static void main(String[] args)throws IOException {
    5. //1.将字节输入流转换成字符输入流
    6. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("./out.txt")));
    7. //2.将字节输出流转换成字符输入流
    8. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./text/file.txt")));
    9. int tempp=0;
    10. while ((tempp=bufferedReader.read())!=-1){
    11. bufferedWriter.write(tempp);
    12. }
    13. //2.将字节输出流转换成字符输入流
    14. BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./text/file.txt",true)));
    15. //1.将字节输出流转换为字符输出流
    16. String str="我是中国人 也是河南人";
    17. bufferedWriter2.write(str);
    18. bufferedReader.close();
    19. bufferedWriter.flush();
    20. bufferedWriter.close();
    21. bufferedWriter2.flush();
    22. bufferedWriter2.close();
    23. }
    24. }