(25)IO流之转换流InputStreamReader和OutputStreamWriter

InputStreamReader:字节到字符的桥梁。

OutputStreamWriter:字符到字节的桥梁。

它们有转换作用,而本身又是字符流。所以在构造的时候,需要传入字节流对象进来。

构造函数:

InputStreamReader(InputStream)

通过该构造函数初始化,使用的是本系统默认的编码表GBK

InputStreamReader(InputStream,String charSet)

通过该构造函数初始化,可以指定编码表。

OutputStreamWriter(OutputStream)

通过该构造函数初始化,使用的是本系统默认的编码表GBK

OutputStreamWriter(OutputStream,String charSet)

通过该构造函数初始化,可以指定编码表。

注意:

操作文件的字符流对象是转换流的子类。属于字节流体系:

Reader

  |--InputStreamReader

  |--FileReader

Writer

  |--OutputStreamWriter

  |--FileWriter

注意:

在使用FileReader操作文本数据时,该对象使用的是默认的编码表。

如果要使用指定编码表时,必须使用转换流。

如果系统默认编码是GBK的:

FileReader fr = new FileReader("a.txt");//操作a.txt的中的数据使用的本系统默认的GBK

操作a.txt中的数据使用的也是本系统默认的GBK

InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));

这两句的代码的意义相同。

但是:如果a.txt中的文件中的字符数据是通过utf-8的形式编码。使用FileReader就无能为力,那么在读取时,就必须指定编码表。那么转换流必须使用。

InputStreamReader isr =

new InputStreamReader(new FileInputStream("a.txt"),"utf-8");

转换流的作用:

1、如果目前所获取到的是一个字节流需要转换成字符流使用,这是就需要使用转换流。把字节流转换成一个字符流

2使用转换流可以指定编码表进行读写文件。 FileReader 不能指定码表,只能使用默认的码表.

使用场景:后面的网络编程就比用到,实际开发中经常见到的问题是,会调用别人的函数,而这些函数往往返回的都是字节流,而不是Reader或者Writer

 1 //使用输入字节流的转换流  指定码表进行读取文件数据
 2     public static void readTest2() throws IOException
 3     {
 4         File file = new File("D:\a.txt");
 5         //创建字节输入流通道
 6         FileInputStream fileInputStream = new FileInputStream(file);
 7         //创建 字节输入字节流的转换流
 8         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
 9         char[] buf = new char[1024];
10         int length = 0;
11         while((length = inputStreamReader.read(buf))!=-1)
12         {
13             System.out.println(new String(buf, 0, length));
14         }
15         inputStreamReader.close();
16         
17     }
18     
19     
20     
21     //使用输出字节流的转换流 指定码表写出数据
22     public static void writeTest2() throws IOException
23     {
24         File file = new File("D:\a.txt");
25         //建立数据的输出通道
26         FileOutputStream fileOutputStream = new FileOutputStream(file);
27         //建立输出字节流转换成字符流 并且指定编码表
28         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
29         outputStreamWriter.write("新中国好");
30         outputStreamWriter.close();
31     }
32     
33     public static void writerTest() throws IOException
34     {
35         File file = new File("D:\a.txt");
36         FileOutputStream fileOutputStream = new FileOutputStream(file);
37         
38         //把输出字节流装换成字符流
39         OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
40         
41         outputStreamWriter.write("大家好");//以往这里使用的是  fileOutputStream.write("大家好".getBytes());
42         
43         outputStreamWriter.close();
44         
45         
46     }
47     
48     public static void readTest() throws IOException
49     {
50 //        Scanner scanner = new Scanner(System.in);
51         InputStream in = System.in;    //获取了标准输入流
52         System.err.println("读到的字符:"+(char)in.read());//read()方法每次只能读入一个字节
53         //需要把字节流转换成字节流,这是据需要使用 转换流
54         
55         InputStreamReader inputStreamReader = new InputStreamReader(in);
56         //使用字符流的缓冲类,一行行的读取数据
57         BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
58         String line = null;
59         while((line = bufferedReader.readLine())!=null)
60         {
61             System.out.println("line:"+line);
62         }
63     }
View Code