生活随笔
收集整理的這篇文章主要介紹了
缓冲流
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
緩沖流
像之前字節流,字符流,使用緩沖區都是自己構造的,而java方法中有自帶的緩沖流,我們如果能夠知道傳輸的文件大小,自己構造合適,如果不知道,使用系統的合適。
使用緩沖流時,實際上是使用了裝飾模式
代碼實例:
public class CharacterCopy1 { public static void main(String
[] args
) {Reader r
= null
;Writer w
= null
;BufferedReader rd
= null
;BufferedWriter wd
= null
;try {r
= new FileReader("d://a.txt");w
= new FileWriter("d://b.txt");StringBuffer strB
= new StringBuffer();
rd
= new BufferedReader(r
);wd
= new BufferedWriter(w
);String l
;while ((l
= rd
.readLine())!=null
) {wd
.write(l
);strB
.append(l
);}System
.out
.println(strB
);} catch (FileNotFoundException e
) {e
.printStackTrace();} catch (IOException e
) {e
.printStackTrace();} finally {try {if (rd
!= null
) rd
.close();if (wd
!= null
) wd
.close();if (r
!= null
) r
.close();if (w
!= null
) w
.close();} catch (IOException e
) {e
.printStackTrace();}}}
}
總結
以上是生活随笔為你收集整理的缓冲流的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。