缓冲流 提高流的读取、写入的速度
BufferIuputStream
BufferOutputStream
package IO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.Test;
public class testBufferInputStream {
@Test
public void BufferStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File f1 = new File("1.png");
File f2 = new File("1_2.png");
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] by = new byte[1024];
while ((len = bis.read(by)) != -1) {
bos.write(by, 0, len);
}
} catch (Exception e) {
}finally{
if (bis != null){
try {
bis.close();
} catch (Exception e) {
}
}
if(bos != null){
try {
bos.close();
} catch (Exception e) {
}
}
}
}
public void copyFile(String input,String output){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File f1 = new File(input);
File f2 = new File(output);
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int len;
byte[] by = new byte[10];
while ((len = bis.read(by)) != -1) {
bos.write(by, 0, len);
}
} catch (Exception e) {
}finally{
if (bis != null){
try {
bis.close();
} catch (Exception e) {
}
}
if(bos != null){
try {
bos.close();
} catch (Exception e) {
}
}
}
}
@Test
public void testCopy(){
long start = System.currentTimeMillis();
copyFile("D:\\桌面文件\\1.mp4","D:\\桌面文件\\3.mp4" );
long end = System.currentTimeMillis();
System.out.println("复制操作花费的时间:"+(end-start));
}
}