1. 程式人生 > >IO BufferedOutputStream和BufferedInputStream

IO BufferedOutputStream和BufferedInputStream

put 方法 是否 spa expimp 填充 stream 設置 偏移

BufferedOutputStream

  FileOutputStream的子類,該類實現緩沖的輸出流。通過設置這種輸出流,應用程序就可以將各個字節寫入基礎輸出流中,而不必為每次字節寫入調用基礎系統。

字段

  protected byte[] buf 存儲數據的內部緩沖區。
  protected int count 緩沖區中的有效字節數。

構造函數

  BufferedOutputStream(OutputStream out) 創建一個新的緩沖輸出流,以將數據寫入指定的基礎輸出流。
  BufferedOutputStream(OutputStream out, int size) 創建一個新的緩沖輸出流,以將具有指定緩沖區大小的數據寫入指定的基礎輸出流。

方法

  void flush() 刷新此緩沖的輸出流。
  void write(byte[] b, int off, int len) 將指定 byte 數組中從偏移量 off 開始的 len 個字節寫入此緩沖的輸出流。
  void write(int b) 將指定的字節寫入此緩沖的輸出流。

BufferedInputStream

  FileInputStream的子類,作為另一種輸入流,BufferedInputStream 為添加了功能,即緩沖輸入和支持 markreset 方法的能力。創建 BufferedInputStream 時即創建了一個內部緩沖區數組。讀取或跳過流中的各字節時,必要時可根據所包含的輸入流再次填充該內部緩沖區,一次填充多個字節。mark

操作記錄輸入流中的某個點,reset 操作導致在從所包含的輸入流中獲取新的字節前,再次讀取自最後一次 mark 操作以來所讀取的所有字節。

字段

  protected byte[] buf 存儲數據的內部緩沖區數組。
  protected int count 比緩沖區中最後一個有效字節的索引大一的索引。
  protected int marklimit 調用 mark 方法後,在後續調用 reset 方法失敗前所允許的最大提前讀取量。
  protected int markpos 最後一次調用 mark 方法時 pos 字段的值。
  protected int pos 緩沖區中的當前位置。

構造函數

  BufferedInputStream(InputStream in) 創建 BufferedInputStream 並保存其參數,即輸入流 in,以便將來使用。
  BufferedInputStream(InputStream in, int size) 創建具有指定緩沖區大小的 BufferedInputStream,並保存其參數,即輸入流 in,以便將來使用。

方法

  int available() 返回可以不受阻塞地從此輸入流讀取的字節數。
  void close() 關閉此輸入流並釋放與該流關聯的所有系統資源。
  void mark(int readlimit) 參見 InputStream 的 mark 方法的常規協定。
  boolean markSupported() 測試此輸入流是否支持 mark 和 reset 方法。
  int read() 參見 InputStream 的 read 方法的常規協定。
  int read(byte[] b, int off, int len) 在此字節輸入流中從給定的偏移量開始將各字節讀取到指定的 byte 數組中。
  void reset() 參見 InputStream 的 reset 方法的常規協定。
  long skip(long n) 參見 InputStream 的 skip 方法的常規協定。

緩沖流復制文件速度對比

public class Test {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        File src = new File("E:\\expimp89\\89.dmp");//文件大小12M
        File desc = new File("F:\\89.dmp");
        //copy_1(src, desc);102412  毫秒
        //copy_2(src, desc);145  毫秒
        //copy_3(src, desc);//471  毫秒
        copy_4(src, desc);//34  毫秒
        long end = System.currentTimeMillis();
        System.out.println((end-start));
    }
    
    //單個字節復制 無緩沖流
    public static void copy_1(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        while((len=fis.read())>-1){
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
    
    //字節數組復制 無緩沖流
    public static void copy_2(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(desc);
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len=fis.read(bytes))>-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
    }
    
    //單個字節復制 緩沖流
    public static void copy_3(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(desc);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        while((len=bis.read())>-1){
            bos.write(len);
        }
        bos.close();
        bis.close();
    }
    
    //字節數組復制 緩沖流
    public static void copy_4(File src,File desc) throws IOException{
        FileInputStream fis = new FileInputStream(src);
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream(desc);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len = 0;
        byte[] bytes = new byte[1024];
        while((len=bis.read(bytes))>-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
    }
}

IO BufferedOutputStream和BufferedInputStream