标签:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class CopyFile {
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String src="C:\\Users\\think\\Desktop\\Java作业4\\a.mp3";
String dest="C:\\Users\\think\\Desktop\\Java作业4\\b.mp3";
CopyFile(src,dest);
long end = System.currentTimeMillis();
System.out.print("花费的时间:" + (end - start)); }
private void CopyFile(String src, String dest) {
//使用缓冲流实现复制
BufferedInputStream bis = null;
BufferedInputStream bos = null; try {
//提供读入写入文件
File file1 = new File(src);
File file2 = new File(dest);
//创建相应的节点流
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos =new FileOutputStream(file2);
//将创建的节点流的对象传递给缓冲就的构造器中
BufferedInputStream bis1 = new BufferedInputStream(fis);
BufferedOutputStream bos1 = new BufferedOutputStream(fos);
//具体的实现文件复制的操作
byte[] b = new byte[1024];
//使用数组来实现传递,用1024个字节传递
int len;
while((len = bis1.read(b)) != -1){
bos1.write(b,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }finally{
//关闭流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis != null){ try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/ying1314/p/5369607.html