生活随笔
收集整理的这篇文章主要介绍了
缓冲流
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
缓冲流
像之前字节流,字符流,使用缓冲区都是自己构造的,而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();}}}
}
总结
以上是生活随笔为你收集整理的缓冲流的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。