【java】学习路径42-六种字符流使用实例

Posted on 2022-05-04 18:27  罗芭Remoo  阅读(53)  评论(0编辑  收藏  举报

第一种 OutputStreamWriter OutputStreamReader

第二种 FileWriter FileReader

第三种 BufferedWriter BufferedReader

共六种字符流,

  一般方便使用中间两种。

  如需设置缓冲流大小,则使用第三种。

  如需设定字符流的编码,则使用第一种。

 

层级关系(Write举例):FileWriter extends InputStreamWriter extends Writer

                       BufferedWriter extends Writer

特别说明:

1、OutputStreamWriter和OutputStreamReader其实也有内置缓冲流,只不过不能自行设置。

2、FileWriter和FileReader本质上是OutputStreamWriter和OutputStreamReader,相当于语法糖。

3、如果要设置OutputStreamWriter和OutputStreamReader的缓冲区大小,我们使用BufferedWriter和BufferedReader。

4、BufferedWriter和BufferedReader可以设置缓冲区大小

 1-OutputStreamWriter

使用该方法构造OutputStreamWriter&InputStreamReader时,需要再手动构造一个FileOutputStream&FileInputStream,比较麻烦

也可以在调用完write方法之后添加如下代码:

outputStreamWriter.flush();

养成良好的编程习惯。

@Test//写到硬盘 OutputStreamWriter
    public void testOutputStreamWriter(){
        OutputStreamWriter outputStreamWriter=null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("字符流输出.txt");
            //可以指定默认编码,GBK,UTF-8,
            outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK");

            outputStreamWriter.write('中');
            outputStreamWriter.write("你好");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStreamWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 2-InputStreamReader

@Test//从硬盘读取 InputStreamReader
public void testInputStreamReader(){
    InputStreamReader inputStreamReader = null;

    try {
        FileInputStream fileInputStream = new FileInputStream("字符流输出.txt");
        //第二个参数 可以指定读取的编码
        inputStreamReader = new InputStreamReader(fileInputStream,"GBK");

        char[] data = new char[1024];
        int length = -1;
        while((length=inputStreamReader.read(data))>=0)
            System.out.print(new String(data,0,length));

    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            inputStreamReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 3-FileWriter

下面这两种没有办法指定编码格式。

@Test//写进硬盘
public void testFileWriter(){
    FileWriter fileWriter = null;
    try {
        fileWriter = new FileWriter("字符流输出.txt");
        fileWriter.write("我使用了FileWriter直接写出字符流,非常方便\n");
        fileWriter.write("不需要像OutputStreamWriter那样\n");

    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 4-FileReader

@Test//从硬盘读取
public void testFileRead(){
    FileReader fileReader = null;

    try {
        fileReader = new FileReader("字符流输出.txt");
        char[] chars = new char[1024];
        int length = -1;
        while((length=fileReader.read(chars))>=0)
            System.out.print(new String(chars,0,length));
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5-BufferedWriter

可以自定缓冲区大小

@Test
public void testBufferedWriter(int bufferedSize){
    BufferedWriter bufferedWriter =null;//定义BufferedWriter
    try {
        //构造一个FileWriter,传给BufferedWriter
        FileWriter fileWriter = new FileWriter("字符流输出.txt");
        //构造BufferedWriter,传入Write类和int类
        bufferedWriter = new BufferedWriter(fileWriter,bufferedSize);
        
        bufferedWriter.write('你');
        bufferedWriter.write("好吖");
        
        bufferedWriter.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 6-BufferedReader

@Test
public void testBufferedReader(){
    BufferedReader bufferedReader = null;
    FileReader fileReader = null;
    try {
        fileReader = new FileReader("字符流输出.txt");
        bufferedReader = new BufferedReader(fileReader);

        //读取单个
        System.out.println((char)bufferedReader.read());
        //读取一整行
        System.out.println(bufferedReader.readLine());
        //剩下的全部
        char[] data = new char[1024];
        int length = -1;
        while((length=bufferedReader.read(data))>=0)
            System.out.println(new String(data,0,length));

    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}