java中講講BufferedInputStream的用法

BufferedInputStream的用法
馬克-to-win:BufferedInputStream 顧名思義就是它有一個內(nèi)部的buffer(緩存),它的read方法表面上看,雖然是只讀了一個字節(jié),但它是開始時猛然從硬盤讀入一大堆字節(jié)到自己的緩存,當(dāng)你read時,它是從緩存讀進一個字節(jié)到內(nèi)存。而前面講的FileInputStream字節(jié)流,read時,都是真正每個字節(jié)都從硬盤到內(nèi)存,是很慢的。為什么?請研究硬盤的結(jié)構(gòu)!下面的兩個例子,一個是FileInputStream的read生讀進來的,另一個是BufferedInputStream的只能read,你比較一下讀的時間,差距蠻大的!馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。



例:2.3.1

import java.io.*;
public class TestMark_to_win {
    public static void main(String args[]) throws FileNotFoundException,
            IOException {
        FileInputStream fis = new FileInputStream("c:/2.txt");
        long t = System.currentTimeMillis();
        int c;
        while ((c = fis.read()) != -1) {}
        fis.close();
        t = System.currentTimeMillis() - t;
        System.out.println("遍歷文件用了如下時間:" + t);
    }
}
結(jié)果是:

遍歷文件用了如下時間:453





例:2.3.2

import java.io.*;
public class TestMark_to_win {
    public static void main(String args[]) throws FileNotFoundException,
            IOException {
        FileInputStream fis = new FileInputStream("i:\\2.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        long t = System.currentTimeMillis();
        int c;
        /*even though the next read() also read one byte, but because
BufferedInputStream has an internal buffer,when first time to read,
it will read in a whole buffer of byte from hard disk, then digest
these bytes one by one in memory */
        while ((c = bis.read()) != -1) {}
        fis.close();
        t = System.currentTimeMillis() - t;
        System.out.println("遍歷文件用了如下時間:" + t);
    }
}
結(jié)果是:
遍歷文件用了如下時間:16


下面的例子講述BufferedInputStream的read(byte b[], int off, int len)的用法。即真正的批量讀入。正好前面講到BufferedOutputStream時,還欠大家一個例子。




例:2.3.3 

import java.io.*;
public class TestMark_to_win {
    public static void main(String[] args) {
        String source = "c:\\2.txt";
        String dest = "c:\\2_copy.txt";
        try {
            FileInputStream fis = new FileInputStream(source);
            BufferedInputStream bis = new BufferedInputStream(fis);
            FileOutputStream fos = new FileOutputStream(dest);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int readcount;
            byte[] readbyte = new byte[256];
            /* public synchronized int read(byte b[], int off, int len) Reads
bytes from this byte-input stream into the specified byte array,
starting at the given offset.
@param b destination buffer.
@param off offset at which to start storing bytes.
@param len maximum number of bytes to read.
@return the number of bytes read, or <code>-1</code> if the end
of the stream has been reached.
@exception IOException if an I/O error occurs.*/
            while ((readcount = bis.read(readbyte, 0, readbyte.length)) != -1) {
                bos.write(readbyte, 0, readcount);
            }
            bis.close();
            bos.close();
            System.out.println("復(fù)制完畢!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

結(jié)果是:

復(fù)制完畢!