位元組緩沖流
1.BufferedOutputStream:該類實作緩沖輸出流,通過設定這樣的輸出流,應用程式可以向底層輸出流寫入位元組,而不必為寫入的每個位元組導致底層系統的呼叫
2.BufferedInputStream:創建BufferedIntputStream將創建一個內部緩沖去陣列,當從流中讀取或者跳過位元組時,內部緩沖去將根據需要從所包含的輸入流中重新填充,一次很多位元組
構造方法:
位元組緩沖輸入流:BufferedOutputStream(OutputStream out):創建一個新的緩沖輸出流,以將資料寫入指定的底層輸出流,
位元組緩沖輸出流:BufferedInputStream(InputStream in):創建一個 BufferedInputStream并保存其引數,輸入流 in供以后使用,
為什么構造方法需要的時位元組流而不是具體的檔案名或者路徑?
因為位元組緩沖流僅僅提供緩沖區,而真正的讀寫資料還得依靠基本的位元組流物件進行操作
public class Demo04 {
public static void main(String[] args) throws IOException {
//創建位元組緩沖輸入流
BufferedOutputStream bops=new BufferedOutputStream(new FileOutputStream("E:\\abc.txt"));
//創建位元組緩沖輸出流
BufferedInputStream bips=new BufferedInputStream(new FileInputStream("E:\\abc.txt"));
//寫資料
bops.write("hello\r\n".getBytes());
bops.write("world\r\n".getBytes());
bops.close();
//讀資料,方式一
int by;
while((by=bips.read())!=-1){
System.out.print((char)by);
}
?
System.out.println("------------------------------------------------");
//方式二 一次讀取一個位元組陣列
byte[]bytes=new byte[1024];
int len;
while((len=bips.read(bytes))!=-1){
System.out.print(new String(bytes,0,len));
}
bips.close();
}
}
?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456983.html
標籤:Java
上一篇:性能優化,實踐淺談
下一篇:二維陣列與稀疏陣列的互轉
