JavaIO学习:缓冲流

缓冲流

1.缓冲流涉及到的类

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter

2.作用

  • 提升流的读取、写入的速度。
  • 原因:内部提供了一个缓冲区,默认情况下是8192,即8kb。

3.使用BufferedInputStream和BufferedOutputStream

处理非文本文件

//实现文件复制的方法
public void copyFileWithBuffered(String srcPath,String destPath) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        //1.
        File srcfile = new File(srcPath);
        File destfile = new File(destPath);

        //2.
        //2.1造节点流
        fis = new FileInputStream(srcfile);
        fos = new FileOutputStream(destfile);

        //2.2造缓冲流
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);

        //3.
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer)) != -1){
            bos.write(buffer,0,len);

      // bos.flush();//刷新缓冲区
        }
        System.out.println("复制成功");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4.要求:先关闭外层的流,再关闭内层的流
        if (bos != null){
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bis != null){
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//说明:关闭外层流的同时,内层流也会自动的进行关闭,关于内层流的关闭,我们可以省略
//   fos.close();
//   fis.close();
}

对上述方法进行测试:

@Test
public void testCopyBuffererd(){
    long start = System.currentTimeMillis();
    String srcPath = "1.jpg";
    String destPath = "6.jpg";
    copyFileWithBuffered(srcPath,destPath);
    long end = System.currentTimeMillis();
    System.out.println("复制所需要的毫秒数为:"+ (end - start));
}

4.使用BufferedReader和BufferedWriter:处理文本文件

//使用BufferedReader和BufferedWriter实现文本文件的复制

@Test
public void test6(){
    BufferedReader br = null;
    BufferedWriter bw = null;

    try {
        //使用匿名方式,创建文件和相应的流
        br = new BufferedReader(new FileReader(new File("dbcp.txt")));
        bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));

        //读写操作
        //方式一:使用char[]数组
//  char[] cbuf = new char[1024];
//   int len;
//   while ((len = br.read(cbuf)) != -1){
//   bw.write(cbuf,0,len);
//   bw.flush();//刷新操作
//   }
        //方式二:使用String
        String data;
        while ((data = br.readLine()) != null){
            bw.write(data);
            bw.newLine();//提供换行的操作
        }
        System.out.println("复制成功");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭资源
        try {
            if (bw != null){
                bw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (br != null){
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/cainiao-chuanqi/p/11328278.html