缓冲流的介绍
package BufferStream;
/*
緩沖流的使用*/
import org.junit.Test;import java.io.*;public class BufferTest {/*實現非文本文件的復制*/@Testpublic void BufferedStreamTest() throws IOException {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {
// 1.造文件對象File srcFile = new File("hanyang.png");File destFile = new File("hangyang2.png");// 2.造字節流FileInputStream fis = new FileInputStream(srcFile);FileOutputStream fos = new FileOutputStream(destFile);// 2.2造緩沖流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);// 3.數據操作byte[] buffer = new byte[5];int len ;while ((len = bis.read(buffer))!=-1){bos.write(buffer,0,len);}} catch (IOException e) {e.printStackTrace();} finally {try {if (bos!=null) {bos.close();}} catch (IOException e) {e.printStackTrace();}try {if (bis!=null) {bis.close();}} catch (IOException e) {e.printStackTrace();}}// 4.資源關閉:先關閉外層,再關閉內層。 注:在關閉外層流的同時,內層流也會自動的關閉// fos.close();
// fis.close();}
}
總結
- 上一篇: python:坦克大战源代码
- 下一篇: 高通工具QXDM安装及使用方法