原文位址:http://blog.csdn.net/lyb1832567496/article/details/52727862
BufferedInputStream的使用
BufferedInputStream:緩沖位元組輸入流,是一個進階流(處理流),與其他低級流配合使用。
構造方法
//建立一個 BufferedInputStream 并儲存其參數,即輸入流 in,以便将來使用。建立一個内部緩沖區數組并将其存儲在 buf 中,該buf的大小預設為8192。
public BufferedInputStream(InputStream in);
//建立具有指定緩沖區大小的 BufferedInputStream 并儲存其參數,即輸入流 in,以便将來使用。建立一個長度為 size 的内部緩沖區數組并将其存儲在 buf 中。
public BufferedInputStream(InputStream in,int size);
從構造方法中我們可以知道BufferedInputStream沒有無參構造方法,它必須傳入一個InputStream(一般是FileInputStream),來一起使用,以提高讀寫效率。
常用的方法
//從該輸入流中讀取一個位元組
public int read();
//從此位元組輸入流中給定偏移量處開始将各位元組讀取到指定的 byte 數組中。
public int read(byte[] b,int off,int len);
從檔案中讀入資料
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
* BufferedInputStream:處理流(進階流),緩沖輸入流
* @author Administrator
*
*/
public class BISDemo01 {
public static void main(String[] args){
try {
FileInputStream fis=new FileInputStream("BISDemo.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
String content=null;
//自己定義一個緩沖區
byte[] buffer=new byte[10240];
int flag=0;
while((flag=bis.read(buffer))!=-1){
content+=new String(buffer, 0, flag);
}
System.out.println(content);
//關閉的時候隻需要關閉最外層的流就行了
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedOutputStream的使用
BufferedOutputStream:緩沖位元組輸出流是一個進階流(處理流),與其他低級流配合使用。
構造方法
//建立一個新的緩沖輸出流,以将資料寫入指定的底層輸出流。
public BufferedOutputStream(OutputStream out);
//建立一個新的緩沖輸出流,以将具有指定緩沖區大小的資料寫入指定的底層輸出流。
public BufferedOutputStream(OutputStream out,int size);
常用的方法
//向輸出流中輸出一個位元組
public void write(int b);
//将指定 byte 數組中從偏移量 off 開始的 len 個位元組寫入此緩沖的輸出流。
public void write(byte[] b,int off,int len);
//重新整理此緩沖的輸出流。這迫使所有緩沖的輸出位元組被寫出到底層輸出流中。
public void flush();
向檔案中寫出資料
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
/**
* BufferedOutputStream:處理流(進階流),緩沖輸出流
* @author Administrator
*
*/
public class BOSDemo01 {
public static void main(String[] args){
try {
FileOutputStream fos=new FileOutputStream("BOSDemo.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
String content="我是緩沖輸出流測試資料!";
bos.write(content.getBytes(),0,content.getBytes().length);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用緩沖輸出流和緩沖輸入流實作檔案的複制
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 使用緩沖輸出流和緩沖輸入流實作檔案的複制
* @author Administrator
*
*/
public class SummaryBISAndBOS {
public static void main(String[] args){
/**
* 1.先将檔案中的内容讀入到緩沖輸入流中
* 2.将輸入流中的資料通過緩沖輸出流寫入到目标檔案中
* 3.關閉輸入流和輸出流
*/
try {
long begin=System.currentTimeMillis();
FileInputStream fis=new FileInputStream("BISDemo.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fos=new FileOutputStream("BOSDemo.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
int size=0;
byte[] buffer=new byte[10240];
while((size=bis.read(buffer))!=-1){
bos.write(buffer, 0, size);
}
//重新整理此緩沖的輸出流,保證資料全部都能寫出
bos.flush();
bis.close();
bos.close();
long end=System.currentTimeMillis();
System.out.println("使用緩沖輸出流和緩沖輸入流實作檔案的複制完畢!耗時:"+(end-begin)+"毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}
}
運作結果:
使用緩沖輸出流和緩沖輸入流實作檔案的複制完畢!耗時:15毫秒
我們可以比較一下這幾種讀寫檔案的方式,我個人感覺基本上沒有多大的差别,個人根據實際情況來使用吧!
以上内容隻代表我個人的觀點,有什麼錯誤的地方請各路大神指正!轉載請注明出處!謝謝!
每天進步一點點!