java-io-flush問題

以前,操作io流的時候,經常看到xxx.flush(),有的人寫,有的人不寫,寫和不寫又沒什麼差別,那麼這個flush()有什麼用呢?程序中需要調用嗎?

自己對這個做了個學習,寫下來方便大家。。。。可能有寫的不對,請大家批評指正

先來看個簡單的demo
public class IOTest {

    public static final int READ_LENGTH = 1024;

    public static void main(String args[]) {

        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\123.txt");//讀取的文件
            File file1 = new File("C:\\Users\\Administrator\\Desktop\\223.txt");//寫入的文件

            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1));

            byte[] b = new byte[READ_LENGTH];

            int temp = 0;

            while ((temp = bufferedInputStream.read(b, 0, READ_LENGTH)) > 0) {

                bufferedOutputStream.write(b, 0, temp);

            }
//          bufferedOutputStream.flush(); 
//這裏註釋掉和不註釋 ,結果都是一樣的,原文件和拷貝的文件都是27.3 KB (28,012 字節),文中數據也一致



        } catch (IOException e) {

        } finally {
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }

                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e1) {

            }

        }

    }

}

然後我們把下面的代碼註釋

//          try {
//              if (bufferedInputStream != null) {
//                  bufferedInputStream.close();
//              }
//
//              if (bufferedOutputStream != null) {
//                  bufferedOutputStream.close();
//              }
//          } catch (IOException e1) {
//
//          }

然後在運行程序,分別看下文件的大小
這裏寫圖片描述

這裏寫圖片描述

原文件的大小27.3 KB (28,012 字節),複製後223.txt 變成了24.0 KB (24,576 字節)

爲什麼成了24,576, 其實仔細算一下,就會發現 24576 正好是1024(代碼中READ_LENGTH)倍數,把READ_LENGTH換成別的數值,也是一樣的道理

問題回來,爲什麼註釋掉close()方法,就發生了這麼大的改變,來看看close()中到底做了什麼?
    /**
     * Closes this output stream and releases any system resources
     * associated with the stream.
     * <p>
     * The <code>close</code> method of <code>FilterOutputStream</code>
     * calls its <code>flush</code> method, and then calls the
     * <code>close</code> method of its underlying output stream.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterOutputStream#flush()
     * @see        java.io.FilterOutputStream#out
     */
    @SuppressWarnings("try")
    public void close() throws IOException {
        try (OutputStream ostream = out) {
            flush();    
        }
    }


    /**
     * Flushes this buffered output stream. This forces any buffered
     * output bytes to be written out to the underlying output stream.
     *
     * @exception  IOException  if an I/O error occurs.
     * @see        java.io.FilterOutputStream#out
     */
    public synchronized void flush() throws IOException {
        flushBuffer();
        out.flush();
    }

    /** Flush the internal buffer */
    private void flushBuffer() throws IOException {
        if (count > 0) {
            out.write(buf, 0, count);
            count = 0;
        }
    }

恍然大悟,即使你不用flush(),在close 方法中也會調用flush(); flush方法其實也就是將緩存區的數據用write方法寫了出去

那麼在程序中,你需要自己顯示的調用flush() 嗎?
我個人覺得還是有必要的,除非你能很確定stream成功的調用close()了!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章