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();}
}