Day26
今天从11点半开始学习到下午四点,专注学习时间大于三个小时了,今晚大概率不去图书馆了,所以下午先把今天的博客敲出来先。IO流学习过程还是比较简单的。Day26了,加油OVO
Day26
duplicated code fragment,代码重复出现的意思。
缓冲流
package com.sorrymaker.IO;
import org.junit.Test;
import java.io.*;
/**
* 缓冲流的作用:提升流的读取,写入的速度。
*/
public class BufferedTest {
/**
实现非文本文件的复制。
*/
@Test
public void BufferedStreamTest() {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File file1 =new File("src/com/sorrymaker/IO/01.png");
File file2 =new File("src/com/sorrymaker/IO/03.png");
//2.1造流
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
//2.2 造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.传数据
byte[] buffer =new byte[10];
int len;
//这里拿bis.read()
while ((len = bis.read(buffer)) != -1) {
//这里用bos
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流资源,这里有关闭顺序的要求,先关闭外层,再关闭内层
//关闭外层的同时,内层流也会自动的关闭,关于内层流的关闭,我们可以省略
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//实现文件复制的方法
public void copyFileWithBuffered(String srcPath,String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.造文件
File srcFile =new File(srcPath);
File destFile =new File(destPath);
