緩衝流

緩衝流

緩衝流的使用

ogLVY.png

緩衝流是處理流的一種html

處理流就是「套接」在已有的流的基礎上java

分類

字節:數組

  • BufferedInputStream
  • BufferedOutputStream

字符:code

  • BufferedReader
  • BufferedWriter

做用

緩衝流的主要做用是提升文件的讀寫效率視頻

BufferedInputStream和BufferedOutputStream的使用

字節型htm

過程

  1. 實例化File類的對象,指明要操做的文件對象

  2. 提供具體的流blog

    2.1 造節點流get

    2.2 造緩衝流input

  3. 數據的寫出輸入過程

  4. 流的關閉操做

    • 關閉順序的要求:先關閉外層的流,再關閉內層的流
  5. try-catch-finally異常處理

說明

  1. 在提供具體的流的時候,先造內層的FileInputStream(FileOutputStream)流,再造外層BufferedInputStream(BufferedOutputStream)流
  2. 在關閉外層流的同時,內層流也會自動的進行關閉。關於內層流的關閉能夠省略

代碼實現

FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
    //1. 實例化File類的對象,指明要操做的文件
    File srcFile = new File("C:\\Users\\LENOVO\\Desktop\\丸子.png");
    File destFile = new File("C:\\Users\\LENOVO\\Desktop\\殷志源.png");
    //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[10];
    int len;
    while((len = bis.read(buffer)) != -1 ){
        bos.write(buffer,0,len);
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    //4. 關閉
    try {
        if(bis != null)
            bis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        if(bos != null)
            bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //關閉內層流省略
//   try {
//        if(fos != null)
//            fos.close();
//        } catch (IOException e) {
//           e.printStackTrace();
//        }
//
//   try {
//        if(fis != null)
//           fis.close();
//        } catch (IOException e) {
//          e.printStackTrace();
//        }
}

運行結果

og8nI.png

緩衝流與節點流讀寫速度對比

兩次代碼的byte[]數組設定的長度相同,都是byte[1024],複製的都是同一個視頻

利用節點流花費的時間爲339(在節點流章節2.2中能夠查看)
http://www.javashuo.com/article/p-ndkwhcha-mx.html

利用緩衝流花費的時間爲77

ogbdG.png

緩衝流提升讀寫速度的緣由

其內部提供了一個緩衝區

文件在讀取的時候先將文件讀取到緩衝區,達到緩衝區指定大小後一次性的寫出


BufferedOutputStream中有一個方法

bos.flush; //刷新緩衝區(會自動刷新,不用顯式調用)

超出緩衝區指定的大小的時候,會自動的刷新出去(把緩衝區的數據狀況,將數據寫出去)

BufferedReader和BufferedWriter的使用

和BufferedInputStream和BufferedOutputStream用法類似

  • BufferedInputStream和BufferedOutputStream「包」的是FileInputStream和FileOutputStream

  • BufferedReader和BufferedWriter「包」的是FileReader和FileWriter

具體緣由見下圖( http://www.javashuo.com/article/p-ewtsqfat-mx.html

ogyr1.png

說明

讀操做的步驟中的兩種寫法

  • 和BufferedInputStream同樣
    char[] cbuf = new char[1024];
    int len;
    while((len = bfr.read(cbuf)) != -1){
        bfw.write(cbuf,0,len);
    }
  • 利用BufferedReader中的readLine()方法和String
    String data;
    while((data = bfr.readLine()) != null){
        bfw.write(data + "\n");
    }
    • readLine():一行一行讀取數據,可是讀取的data中不包含換行符,若是隻是以下這樣寫,最後複製的結果全部文字都在一行

      bfw.write(data );

      • 添加換行的方法一:
        加上換行符(如上面列舉的例子)
      • 添加換行的方法二:newLine()方法(BufferedWriter中的方法)
      bfw.write(data);
      bfw.newLine();

代碼實現

文本文件的複製

public void copytxt(String srcPath, String destPath){
    BufferedReader bfr = null;
    BufferedWriter bfw = null;
    try {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        bfr = new BufferedReader(new FileReader(srcFile));
        bfw = new BufferedWriter(new FileWriter(destFile));

        //方式一
        // char[] cbuf = new char[1024];
        // int len;
        // while((len = bfr.read(cbuf)) != -1){
        //     bfw.write(cbuf,0,len);
        // }

        //方式二
        String data;
        while((data = bfr.readLine()) != null){
            //2.1
//       bfw.write(data + "\n");//data中不包含換行符
            //2.2
            bfw.write(data);
            bfw.newLine();
        }

        System.out.println("複製成功");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(bfr != null)
                bfr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            if(bfw != null)
                bfw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Test
public void test6(){
    String srcPath = "C:\\Users\\LENOVO\\Desktop\\hello.txt";
    String destPath = "C:\\Users\\LENOVO\\Desktop\\hello(1).txt";

    long start = System.currentTimeMillis();

    copytxt(srcPath,destPath);

    long end = System.currentTimeMillis();

    System.out.println("花費的時間:" + (end - start));
}
相關文章
相關標籤/搜索