Java输入输出流六:字符流之InputStreamReader和OutputStreamWriter

●字符流中常用的两个是:InputStreamReader和OutputStreamWriter;这两个是不带缓冲区的;

●InputStreamReader和OutputStreamWriter是字节字符转换流,这两个类的作用就是把字节流转换成字符流;

●InputStreamReader和OutputStreamWriter,在涉及到网络传输时较为常用,因为网络上传输的数据都是字节流,本地接受后,如有需要可以通过InputStreamReader和OutputStreamWriter将接受过来的字节流转成字符流。

除了InputStreamReader和OutputStreamWriter,当不涉及到网路传入或其他场景时,可以通过字符流中的另外两个子类,FileReader和FileWriter,直接把数据读成字符流


一:InputStreamReader类和OutputStreamWriter类,构造方法,常用方法简介

InputStreamReader类:

构造方法:参数是字节输入流,因为InputStreamReader的作用就是把字节输入流转换成字符

常用方法:

……………………………………………………

OutputStreamWriter类:

构造方法:

常用方法:


二:InputStreamReader示例

InputStreamReader类,调用read()方法读取数据并打印;

(1)构造InputStreamReader对象时,需要FileInputStream对象;即构造字符输入流对象,需要用到字节输入流对象;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class ReaderDemo {

    public static void main(String[] args) {
       
        try {
            // (1)先用字节输入流读取数据,得到文件的字节流(后续再转成字符流)
            FileInputStream fis = new FileInputStream("imooc.txt");
            // (2)将字节流转成字符流,这一步是流的连接
            InputStreamReader isr = new InputStreamReader(fis);
            int n = 0;
            // (3)定义字符数组,存放读取到的字符
            char[] cbuf = new char[10];
            while((n=isr.read())!=-1){     // 这个read()方法的返回值是实际读取到的值
                System.out.print((char)n);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

InputStreamReader类,调用read()方法读取数据并打印;使用read(char b)方法读取:和上例一样,只是使用了read方法的另一种重载形式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class ReaderDemo {

    public static void main(String[] args) {
       
        try {
            // (1)先用字节输入流读取数据,得到文件的字节流(后续再转成字符流)
            FileInputStream fis = new FileInputStream("imooc.txt");
            // (2)将字节流转成字符流,这一步是流的连接
            InputStreamReader isr = new InputStreamReader(fis);
            int n = 0;
            // (3)定义字符数组,存放读取到的字符
            char[] cbuf = new char[10];
            // 调用另一种read(char b)方法
            while((n = isr.read(cbuf))!= -1){    // 这个read(char b)方法的返回值,是读取字符的数量
                String s = new String(cbuf,0,n);
                System.out.print(s);
            }
           
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}


三:InputStreamReader和OutputStreamWriter示例,文件复制

(1)InputStreamReader和OutputStreamWriter对象的构建,都需要相应的字节流对象;

(2)InputStreamReader类的read方法和OutputStreamWriter类的write方法相互对应,根据业务需求选用;

(3)这儿自所以写成字节流转成字符流的样子,只是为了模拟网络传输的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class ReaderDemo1 {
    public static void main(String[] args) {
       
        try {
            // (1)先用字节输入流读取数据,得到文件的字节流(后续再转成字符流)
            FileInputStream fis = new FileInputStream("imooc.txt");
            FileOutputStream fos = new FileOutputStream("imooc1.txt");
            // (2)将字节流转成字符流,这一步是流的连接
            InputStreamReader isr = new InputStreamReader(fis);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
           
            int n = 0;
            // (3)定义字符数组,存放读取到的字符
            char[] cbuf = new char[10];
            // (4)读写文件
            while((n = isr.read(cbuf))!= -1){
                osw.write(cbuf, 0, n);   // OutputStreamWriter类的write()方法有很多重载形式,可按需求选用
            }
            // (5)flush并关闭流
            osw.flush();
            fis.close();
            fos.close();
            isr.close();
            osw.close();   
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

上面也可指定字符编码: