使用BufferReader与BufferWriter读写文件

使用BufferReader与BufferWriter读写文件,第1张

BufferReader与BufferWriter简介
BufferReader与BufferWriter自JDK1.1就已经存在了.
BufferReader的作用是从字符输入流读取文本字符到字符缓冲区中,以便提供对字符、数组和行的高效读取.
BufferWriter的作用是将文本写入字符输出流到字符缓冲区,以便提供对单个字符、数组和字符串的有效写入.

在 *** 作文件时,为了获得最高效率,一般考虑在BufferReader,BufferedWriter中包装OutputStreamWriter,以避免频繁调用字符编码器StreamEncoder的implWrite方法

BufferWriter类中的方法如下

BufferReader类中的方法如下

查看源码可知,BufferReader与BufferWriter默认的字符缓冲区(defaultCharBufferSize )的大小均为8192个字符,一个字符占用两个字节的空间,所以缓冲区的大小是16kb.
BufferWriter类的基本属性如下所示:

 	//字符输出流
    private Writer out;
 
    //字符缓冲区
    private char cb[];
    
    //设置的字符缓冲区大小变量
    private int nChars;
    
    //字符缓冲区中的已存储元素的位置
    private int  nextChar;
 
    //默认字符缓冲区大小
    private static int defaultCharBufferSize = 8192;
 
    //行分割字符串
    private String lineSeparator;

1.使用BufferWriter写入BufferReader读入的数据示列:

public static void main(String[] args) {
        String inPath = "F:\io_test_file\test.txt";
        String outPath = "F:\io_test_file\mean.txt";
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inPath),"UTF-8"));
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath),"UTF-8"))){
            String line = null;
            LocalDateTime starTime = LocalDateTime.now();
            while ((line = bufferedReader.readLine()) != null) {
                bufferedWriter.write(line);
                bufferedWriter.flush();
            }
            System.out.println("耗时:" + Duration.between(starTime, LocalDateTime.now()).toMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

读取的文本文件test.txt的大小是1139kb,但是上面程序执行结果输出的mean.txt的大小是1120kb,原因是如果没有调用newLine()方法,默认是写满文本一行的空间大小后,再换行追加写入,所以,是少了每一行的\r\n两个字符,各占两个字节,共四个字节,即一行会少四个字节大小的空间.

2.然后我验证了一下,在write(line)方法中拼接\r\n进行写入后,发现mean.txt文本的大小与test.txt的文本大小一致了.

public static void main(String[] args) {
        String inPath = "F:\io_test_file\test.txt";
        String outPath = "F:\io_test_file\mean.txt";
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inPath),"UTF-8"));
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath),"UTF-8"))){
            String line = null;
            LocalDateTime starTime = LocalDateTime.now();
            while ((line = bufferedReader.readLine()) != null) {
                bufferedWriter.write(line+"\r\n");
                bufferedWriter.flush();
            }
            System.out.println("耗时:" + Duration.between(starTime, LocalDateTime.now()).toMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.再去掉2中拼接的\r\n后,在写入方法write(line)方法的后面调用BufferWrite()的newLine()方法,得到的mean.txt文本的大小也与test.txt文本的大小一致.

public static void main(String[] args) {
        String inPath = "F:\io_test_file\test.txt";
        String outPath = "F:\io_test_file\mean.txt";
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inPath),"UTF-8"));
             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPath),"UTF-8"))){
            String line = null;
            LocalDateTime starTime = LocalDateTime.now();
            while ((line = bufferedReader.readLine()) != null) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
            System.out.println("耗时:" + Duration.between(starTime, LocalDateTime.now()).toMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

注:实例采用的是字符流的方式读入写入的方式,对于文本文件是不存在文件读写出错的,一旦采用上面的字符读写方式区 *** 作word就不行了,有两个原因,一个是word的编码是UTF-16,二是处理要求二进制格式,就不可能用字符流而必须是字节流。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/876858.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存