Java IO(4)

轉載:http://www.cnblogs.com/zhaoyanjun/p/6376937.html
   http://blog.csdn.net/dabing69221/article/details/16996877

InputStream
|__FilterInputStream
        |__BufferedInputStream

首先拋出一個問題,有了InputStream為什么還要有BufferedInputStream?

BufferedInputStream和BufferedOutputStream這兩個類分別是FilterInputStream和FilterOutputStream的子類,作為裝飾器子類,使用它們可以防止每次讀取/發送數據時進行實際的寫操作,代表著使用緩沖區。

我們有必要知道不帶緩沖的操作,每讀一個字節就要寫入一個字節,由于涉及磁盤的IO操作相比內存的操作要慢很多,所以不帶緩沖的流效率很低。帶緩沖的流,可以一次讀很多字節,但不向磁盤中寫入,只是先放到內存里。等湊夠了緩沖區大小的時候一次性寫入磁盤,這種方式可以減少磁盤操作次數,速度就會提高很多!

同時正因為它們實現了緩沖功能,所以要注意在使用BufferedOutputStream寫完數據后,要調用flush()方法或close()方法,強行將緩沖區中的數據寫出。否則可能無法寫出數據。與之相似還BufferedReader和BufferedWriter兩個類。

現在就可以回答在本文的開頭提出的問題:

BufferedInputStream和BufferedOutputStream類就是實現了緩沖功能的輸入流/輸出流。使用帶緩沖的輸入輸出流,效率更高,速度更快。

總結:
BufferedInputStream 是緩沖輸入流。它繼承于FilterInputStream。

BufferedInputStream 的作用是為另一個輸入流添加一些功能,例如,提供“緩沖功能”以及支持mark()標記和reset()重置方法。

BufferedInputStream 本質上是通過一個內部緩沖區數組實現的。例如,在新建某輸入流對應的BufferedInputStream后,當我們通過read()讀取輸入流的數據時,BufferedInputStream會將該輸入流的數據分批的填入到緩沖區中。每當緩沖區中的數據被讀完之后,輸入流會再次填充數據緩沖區;如此反復,直到我們讀完輸入流數據位置。

BufferedInputStream API簡介
源碼關鍵字段分析

private static int defaultBufferSize = 8192;//內置緩存字節數組的大小 8KB
    
protected volatile byte buf[];  //內置緩存字節數組
    
protected int count;    //當前buf中的字節總數、注意不是底層字節輸入流的源中字節總數
    
protected int pos;      //當前buf中下一個被讀取的字節下標
    
protected int markpos = -1; //最后一次調用mark(int readLimit)方法記錄的buf中下一個被讀取的字節的位置
    
protected int marklimit;    //調用mark后、在后續調用reset()方法失敗之前云尋的從in中讀取的最大數據量、用于限制被標記后buffer的最大值

構造函數

BufferedInputStream(InputStream in) //使用默認buf大小、底層字節輸入流構建bis 

BufferedInputStream(InputStream in, int size) //使用指定buf大小、底層字節輸入流構建bis  

一般方法介紹

int available();  //返回底層流對應的源中有效可供讀取的字節數      
  
void close();  //關閉此流、釋放與此流有關的所有資源  
  
boolean markSupport();  //查看此流是否支持mark
  
void mark(int readLimit); //標記當前buf中讀取下一個字節的下標  
  
int read();  //讀取buf中下一個字節  
  
int read(byte[] b, int off, int len);  //讀取buf中下一個字節  
  
void reset();   //重置最后一次調用mark標記的buf中的位子  
  
long skip(long n);  //跳過n個字節、 不僅僅是buf中的有效字節、也包括in的源中的字節 

BufferedOutputStream API簡介
關鍵字段

protected byte[] buf;   //內置緩存字節數組、用于存放程序要寫入out的字節  
  
protected int count;   //內置緩存字節數組中現有字節總數 

構造函數

BufferedOutputStream(OutputStream out); //使用默認大小、底層字節輸出流構造bos。默認緩沖大小是 8192 字節( 8KB )
  
BufferedOutputStream(OutputStream out, int size);  //使用指定大小、底層字節輸出流構造bos  

構造函數源碼:

/**
  * Creates a new buffered output stream to write data to the
  * specified underlying output stream.
  * @param   out   the underlying output stream.
  */
 public BufferedOutputStream(OutputStream out) {
     this(out, 8192);
 }

 /**
  * Creates a new buffered output stream to write data to the
  * specified underlying output stream with the specified buffer
  * size.
  *
  * @param   out    the underlying output stream.
  * @param   size   the buffer size.
  * @exception IllegalArgumentException if size <= 0.
  */
 public BufferedOutputStream(OutputStream out, int size) {
     super(out);
     if (size <= 0) {
         throw new IllegalArgumentException("Buffer size <= 0");
     }
     buf = new byte[size];
 }

一般方法

//在這里提一句,`BufferedOutputStream`沒有自己的`close`方法,當他調用父類`FilterOutputStrem`的方法關閉時,會間接調用自己實現的`flush`方法將buf中殘存的字節flush到out中,再`out.flush()`到目的地中,DataOutputStream也是如此。 
  
void  flush();  將寫入bos中的數據flush到out指定的目的地中、注意這里不是flush到out中、因為其內部又調用了out.flush()  
  
write(byte b);      將一個字節寫入到buf中  
  
write(byte[] b, int off, int len);      將b的一部分寫入buf中 

那么什么時候flush()才有效呢?
答案是:當OutputStream是BufferedOutputStream時。

當寫文件需要flush()的效果時,需要
FileOutputStream fos = new FileOutputStream("c:\a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
也就是說,需要將FileOutputStream作為BufferedOutputStream構造函數的參數傳入,然后對BufferedOutputStream進行寫入操作,才能利用緩沖及flush()。

查看BufferedOutputStream的源代碼,發現所謂的buffer其實就是一個byte[]。
BufferedOutputStream的每一次write其實是將內容寫入byte[],當buffer容量到達上限時,會觸發真正的磁盤寫入。
而另一種觸發磁盤寫入的辦法就是調用flush()了。

1.BufferedOutputStream在close()時會自動flush
2.BufferedOutputStream在不調用close()的情況下,緩沖區不滿,又需要把緩沖區的內容寫入到文件或通過網絡發送到別的機器時,才需要調用flush.

實戰演練1:復制文件.
操作:使用緩存流將F盤根目錄里面名字為:123.png 圖片復制成 abc.png

package com.app;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class A3 {

    public static void main(String[] args) throws IOException {

        String filePath = "F:/123.png" ;
        String filePath2 = "F:/abc.png" ;
        File file = new File( filePath ) ;
        File file2 = new File( filePath2 ) ;
        copyFile( file , file2 );

    }
    
    /**
     * 復制文件
     * @param oldFile
     * @param newFile
     */
    public static void copyFile( File oldFile , File newFile){
        InputStream inputStream = null ;
        BufferedInputStream bufferedInputStream = null ;

        OutputStream outputStream = null ;
        BufferedOutputStream bufferedOutputStream = null ;

        try {
            inputStream = new FileInputStream( oldFile ) ;
            bufferedInputStream = new BufferedInputStream( inputStream ) ;

            outputStream = new FileOutputStream( newFile ) ;
            bufferedOutputStream = new BufferedOutputStream( outputStream ) ;

            byte[] b=new byte[1024];   //代表一次最多讀取1KB的內容

            int length = 0 ; //代表實際讀取的字節數
            while( (length = bufferedInputStream.read( b ) )!= -1 ){
                //length 代表實際讀取的字節數
                bufferedOutputStream.write(b, 0, length );
            }
            //緩沖區的內容寫入到文件
            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {

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

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

        }
    }
}

如何正確的關閉流

在上面的代碼中,我們關閉流的代碼是這樣寫的。

finally {

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

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

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

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

        }

思考:在處理流關閉完成后,我們還需要關閉節點流嗎?

讓我們帶著問題去看源碼:

bufferedOutputStream.close();
 /**
 * Closes this input stream and releases any system resources
 * associated with the stream.
 * Once the stream has been closed, further read(), available(), reset(),
 * or skip() invocations will throw an IOException.
 * Closing a previously closed stream has no effect.
 *
 * @exception  IOException  if an I/O error occurs.
 */
public void close() throws IOException {
    byte[] buffer;
    while ( (buffer = buf) != null) {
        if (bufUpdater.compareAndSet(this, buffer, null)) {
            InputStream input = in;
            in = null;
            if (input != null)
                input.close();
            return;
        }
        // Else retry in case a new buf was CASed in fill()
    }
}

close()方法的作用
1、關閉輸入流,并且釋放系統資源
2、BufferedInputStream裝飾一個 InputStream 使之具有緩沖功能,is要關閉只需要調用最終被裝飾出的對象的 close()方法即可,因為它最終會調用真正數據源對象的 close()方法。因此,可以只調用外層流的close方法關閉其裝飾的內層流。

那么如果我們想逐個關閉流,我們該怎么做?

答案是:先關閉外層流,再關閉內層流。一般情況下是:先打開的后關閉,后打開的先關閉;另一種情況:看依賴關系,如果流a依賴流b,應該先關閉流a,再關閉流b。例如處理流a依賴節點流b,應該先關閉處理流a,再關閉節點流b

看懂了怎么正確的關閉流之后,那么我們就可以優化上面的代碼了,只關閉外層的處理流。

finally {

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

            if( bufferedInputStream != null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
為什么要flush

與在網絡硬件中緩存一樣,流還可以在軟件中得到緩存,即直接在Java代碼中緩存。這可以通過BufferedOutputStream或BufferedWriter 鏈接到底層流上來實現。因此,在寫完數據時,flush就顯得尤為重要。比如:


上圖中WEB服務器通過輸出流向客戶端響應了一個300字節的信息,但是,這時的輸出流有一個1024字節的緩沖區。所以,輸出流就一直等著WEB服務器繼續向客戶端響應信 息,當WEB服務器的響應信息把輸出流中的緩沖區填滿時,這時,輸出流才向WEB客戶端響應消息。
為了解決這種尷尬的局面,flush()方法出現了。flush()方法可以強迫輸出流(或緩沖的流)發送數據,即使此時緩沖區還沒有填滿,以此來打破這種死鎖的狀態。
當我們使用輸出流發送數據時,當數據不能填滿輸出流的緩沖區時,這時,數據就會被存儲在輸出流的緩沖區中。如果,我們這個時候調用關閉(close)輸出流,存儲在輸出流的緩沖區中的數據就會丟失。所以說,關閉(close)輸出流時,應先刷新(flush)換沖的輸出流,話句話說就是:“迫使所有緩沖的輸出數據被寫出到底層輸出流中”。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,732評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,214評論 3 426
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,781評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,588評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,315評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,699評論 1 327
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,698評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,882評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,441評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,189評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,388評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,933評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,613評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,023評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,310評論 1 293
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,112評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,334評論 2 377

推薦閱讀更多精彩內容

  • # 3.1 File # ## 3.1.1 File基本概念 ## 1.基本概念 -File類用于表示文件(目錄)...
    閆子揚閱讀 482評論 0 0
  • 1 IO(二)No19 【 緩沖流:內置了緩沖區,對現有的流對象進行了封裝,實現了高效的讀寫操作并增強了功能 ...
    征程_Journey閱讀 722評論 0 1
  • 如果想你了, 我會掏出手機,看看有沒有你的微信, 即使我知道,機率是那么的渺茫。 如果想你了, 我會用拇指在手機上...
    靜寂夜雨閱讀 202評論 0 0
  • 昨天無意中看到你的作文《媽媽,我想對您說》,看了以后心里滿不是滋味的。原來我己經悄悄地“傷害”著你了,雖然我一直都...
    懶散的我閱讀 351評論 0 0
  • 楊絳說:你的問題是想得太多,而看的太少。這一直激勵著我堅持看書,努力提高自己的思想水平。也讓自己變得想法多變,邏輯...
    方鴻漸加油努力奮斗閱讀 1,186評論 2 1