IO流之緩沖流

上一篇:IO流之文件字符輸出流【FileWriter】 http://www.itdecent.cn/p/579ea78e5bae

1、概述

緩沖流,也叫高效流,是對4個(gè)基本的 FileXxx 流的增強(qiáng),所以也是4個(gè)流,按照數(shù)據(jù)類型分類:

  • 字節(jié)緩沖流: BufferedInputStream、BufferedOutputStream
  • 字符緩沖流: BufferedReader、BufferedWriter
    緩沖流的基本原理,是在創(chuàng)建流對象時(shí),會創(chuàng)建一個(gè)內(nèi)置的默認(rèn)大小的緩沖區(qū)數(shù)組,通過緩沖區(qū)讀寫,減少系統(tǒng)IO 次數(shù),從而提高讀寫的效率。

2、字節(jié)緩沖流

2.1 BufferedOutputStream【字節(jié)緩沖輸出流】

BufferedOutputStream extends FilterOutputStream extends OutputStream
繼承自父類的成員方法:

  • void close():關(guān)閉此輸出流并釋放與此流相關(guān)聯(lián)的任何系統(tǒng)資源。
  • void flush():刷新此輸出流并強(qiáng)制任何緩沖的輸出字節(jié)被寫出。
  • void write(byte[] b):將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流。
  • void write(byte[] b, int off, int len):從指定的字節(jié)數(shù)組寫入 len個(gè)字節(jié),從偏移 off開始輸出到此輸出流。
  • abstract void write(int b):將指定的字節(jié)寫入此輸出流。
2.1.1 構(gòu)造方法
  • BufferedOutputStream(OutputStream out)
    創(chuàng)建一個(gè)新的緩沖輸出流,以將數(shù)據(jù)寫入指定的底層輸出流。
  • BufferedOutputStream(OutputStream out, int size)
    創(chuàng)建一個(gè)新的緩沖輸出流,以便以指定的緩沖區(qū)大小將數(shù)據(jù)寫入指定的底層輸出流
2.1.2 參數(shù):
  • OutputStream out:字節(jié)輸出流
  • int size:指定緩沖區(qū)內(nèi)部緩沖區(qū)的大小,默認(rèn)值為:8192
    構(gòu)造舉例,代碼如下:
//創(chuàng)建文件字節(jié)輸出流
 FileOutputStream os = new FileOutputStream("IO流//e.txt");
// 創(chuàng)建字節(jié)緩沖輸出流 
BufferedOutputStream bos= new BufferedOutputStream(os);
BufferedOutputStream bos2= new BufferedOutputStream(os,5);
2.1.3、寫出數(shù)據(jù)
public static void main(String[] args) throws Exception {
        //創(chuàng)建FileOutputStream對象,構(gòu)造方法中綁定要輸出的目的地
        FileOutputStream os = new FileOutputStream("IO流//e.txt");
        //創(chuàng)建BufferedOutputStream對象,構(gòu)造方法中傳遞FileOutputStream對象,提高FileOutputStream對象效率
        BufferedOutputStream bos = new BufferedOutputStream(os);
        //使用BufferedOutputStream對象中的方法write方法,把數(shù)據(jù)寫入到內(nèi)部緩沖區(qū)中
        bos.write("測試字節(jié)緩沖輸出流".getBytes());
        //把內(nèi)部緩沖區(qū)中的數(shù)據(jù),刷新到文件中
        bos.flush();
        //是否資源(會先把內(nèi)存緩沖區(qū)的數(shù)據(jù),刷新到文件中)
        bos.close();
    }

2.2 BufferedInputStream【字節(jié)緩沖輸入流】

BufferedInputStream extends FilterInputStream extends InputStream
繼承自父類的成員方法:

  • void close():關(guān)閉此輸入流并釋放與流相關(guān)聯(lián)的任何系統(tǒng)資源。
  • abstract int read():從輸入流讀取數(shù)據(jù)的下一個(gè)字節(jié)。
  • int read(byte[] b):從輸入流讀取一些字節(jié)數(shù),并將它們存儲到緩沖區(qū) b 。
  • int read(byte[] b, int off, int len):從輸入流讀取最多 len字節(jié)的數(shù)據(jù)到一個(gè)字節(jié)數(shù)組。
2.2.1 構(gòu)造方法
  • BufferedInputStream(InputStream in)
    創(chuàng)建一個(gè) BufferedInputStream并保存其參數(shù),輸入流 in ,供以后使用。
  • BufferedInputStream(InputStream in, int size)
    創(chuàng)建 BufferedInputStream具有指定緩沖區(qū)大小,并保存其參數(shù),輸入流 in ,供以后使用。
2.2.2 參數(shù):
  • InputStream in:字節(jié)輸入流
  • int size:指定緩沖區(qū)內(nèi)部緩沖區(qū)的大小,默認(rèn)值為:8192
    構(gòu)造舉例,代碼如下:
//創(chuàng)建文件字節(jié)輸入流
  FileInputStream fs = new FileInputStream("IO流\\e.txt");
//創(chuàng)建字節(jié)緩沖輸入流
  BufferedInputStream bis = new BufferedInputStream(fs);
  BufferedInputStream bis2 = new BufferedInputStream(fs,5);
2.2.3 讀取數(shù)據(jù)
//創(chuàng)建FileInputStream對象,綁定要讀取的數(shù)據(jù)源
        FileInputStream fs = new FileInputStream("IO流\\e.txt");
        //創(chuàng)建BufferedInputStream對象,構(gòu)造方法中傳遞FileInputStream對象,提高FileInputStream對象的讀取效率
        BufferedInputStream bis = new BufferedInputStream(fs);
        //存儲每次讀取的數(shù)據(jù)
        byte[] bytes = new byte[102];
        int len = 0; //記錄每次讀取的有效字節(jié)個(gè)數(shù)
        //使用BufferedInputStream對象中的方法read,讀取文件
        while((len = bis.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
        }
        bis.close();
        fs.close();
    }

效率測試
查詢API,緩沖流讀寫方法與基本的流是一致的,我們通過復(fù)制大文件(79.2 MB ),測試它的效率。
1. 基本流,代碼如下:

 public static void main(String[] args) throws Exception{
        long startTime = System.currentTimeMillis();
        //創(chuàng)建FileInputStream對象
        FileInputStream fis = new FileInputStream("D:\\dev\\1.jpg");
        //創(chuàng)建FileOutputStream對象
        FileOutputStream fos = new FileOutputStream("D:\\dev\\2.jpg");
        //一次讀取多個(gè)字節(jié)的方式
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = fis.read(bytes)) != -1){
            //使用方法write(),將讀取的流寫入到目的地的文件中
            fos.write(bytes,0,len);
        }
        //釋放資源,先關(guān)閉寫的(如果寫完了,肯定讀完了)
        fos.close();
        fis.close();
        long endTime = System.currentTimeMillis();
        System.out.println("文件復(fù)制共耗時(shí):" + (endTime - startTime) + "毫秒");
    }

輸出結(jié)果:文件復(fù)制共耗時(shí):2103毫秒

2. 緩沖流,代碼如下:

 public static void main(String[] args) throws Exception {
        long startTime = System.currentTimeMillis();
        //創(chuàng)建流對象
        try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\安裝包\\SecureCRT72.zip"));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\安裝包\\SecureCRT744442.zip"));)
        {
            // 讀寫數(shù)據(jù)
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = bis.read(b)) != -1){
                bos.write(b,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        long endTime = System.currentTimeMillis();
        System.out.println("緩沖流復(fù)制時(shí)間:"+(endTime - startTime)+" 毫秒");
    }


輸出結(jié)果:緩沖流復(fù)制時(shí)間:378 毫秒

2.3 BufferedWriter【字符緩沖輸出流】

BufferedWriter extends Writer
繼承自父類的成員方法:

  • abstract void close():關(guān)閉流,但要先刷新。
  • abstract void flush():刷新流。
  • void write(int c):寫一個(gè)字符
  • void write(char[] cbuf):寫入一個(gè)字符數(shù)組。
  • void write(char[] cbuf, int off, int len):寫入字符數(shù)組的一部分。
  • void write(String str):寫一個(gè)字符串
  • void write(String s, int off, int len):寫一個(gè)字符串的一部分。
    特有成員方法:
  • void newLine:寫一行行分隔符。 會根據(jù)不同的操作系統(tǒng),獲取不同的行分隔符。win:\r\n linux:\n mac:\r
2.3.1 構(gòu)造方法
  • BufferedWriter(Writer out)
    創(chuàng)建使用默認(rèn)大小的輸出緩沖區(qū)的緩沖字符輸出流。
  • BufferedWriter(Writer out, int sz)
    創(chuàng)建一個(gè)新的緩沖字符輸出流,使用給定大小的輸出緩沖區(qū)。
2.3.2 參數(shù):

Writer out:字符輸出流
int sz:指定緩沖區(qū)內(nèi)部緩沖區(qū)的大小,默認(rèn)值為:8192
構(gòu)造舉例,代碼如下:

//創(chuàng)建FileWriter文件字符輸出流對象
 FileWriter fw = new FileWriter("IO流\\f.txt");
//創(chuàng)建字符緩存輸出流對象
 BufferedWriter bw = new BufferedWriter(fw);
 BufferedWriter bw2 = new BufferedWriter(fw,6);

2.3.3 寫數(shù)據(jù)

  public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("IO流\\f.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        for (int i = 0; i < 10; i++) {
            bw.write("你好棒哦");
            //換行
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }


輸出結(jié)果:
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦
你好棒哦

2.4 BufferedReader【字符緩沖輸入流】

BufferedReader extends Reader
繼承自父類的成員方法:

  • void close():關(guān)閉流并釋放與之相關(guān)聯(lián)的任何系統(tǒng)資源。
  • int read():讀一個(gè)字符
  • int read(char[] cbuf, int off, int len):將字符讀入數(shù)組的一部分。
    特有成員方法:
    String readLine:讀一行文字。一行被視為由換行符('\ n'),回車符('\ r')中的任何一個(gè)或隨后的換行符終止。
    返回值:
    包含行的內(nèi)容的字符串,不包括任何行終止字符,如果已達(dá)到流的末尾,則為null
2.3.1 構(gòu)造方法
  • BufferedReader(Reader in)
    創(chuàng)建使用默認(rèn)大小的輸入緩沖區(qū)的緩沖字符輸入流。
  • BufferedReader(Reader in, int sz)
    創(chuàng)建使用指定大小的輸入緩沖區(qū)的緩沖字符輸入流。
2.3.2 參數(shù)
  • Reader in:字符輸入流
  • int sz:指定緩沖區(qū)內(nèi)部緩沖區(qū)的大小,默認(rèn)值為:8192
    構(gòu)造舉例,代碼如下:
//創(chuàng)建文件字符輸入流
  FileReader fr = new FileReader("IO流\\f.txt");
 //創(chuàng)建字符緩沖輸入流
  BufferedReader br = new BufferedReader(fr);
  BufferedReader br2 = new BufferedReader(fr,5);
2.3.3 讀取數(shù)據(jù)

使用readLine方法讀

   public static void main(String[] args) throws Exception {
        //// 創(chuàng)建流對象
        FileReader fr = new FileReader("IO流\\f.txt");
        BufferedReader br = new BufferedReader(fr);
      // 定義字符串,保存讀取的一行文字
        String line = null;
     // 循環(huán)讀取,讀取到最后返回null
        while ((line = br.readLine()) != null){
            System.out.println(line);
        }
      // 釋放資源 
      br.close();
    }

下一篇:IO流之轉(zhuǎn)換流 http://www.itdecent.cn/p/bcf14a231b65

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容