没有优化前的文件拷贝
package com.insigma.chapter13;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileCopyDemo {
/**
* 文件拷贝
* @param args
*/
public static void main(String[] args) {
long start = System. currentTimeMillis();
//文件源路径
String sourcePath="F:/me/2013-09-04_132030.png";
//要拷贝到的路径
String targetPath="F:/copy.png";
File ifile=new File(sourcePath);
File ofile=new File(targetPath);
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream=new FileInputStream(ifile);
fileOutputStream=new FileOutputStream(ofile);
//读取文件
int rel=-1;
while((rel=fileInputStream.read())!=-1){
fileOutputStream.write(rel);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System. currentTimeMillis();
long time=end-start;
System.out.println(time);
}
}
使用装饰流BufferedInputStream来进行优化,
BufferedInputStream
时,会创建一个内部缓冲区数组。
package com.insigma.chapter13;
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 java.io.InputStream;
public class FileCopyDemo {
/**
* 文件拷贝
* @param args
*/
public static void main(String[] args) {
long start = System. currentTimeMillis();
//文件源路径
String sourcePath="F:/me/2013-09-04_132030.png";
//要拷贝到的路径
String targetPath="F:/copy.png";
File ifile=new File(sourcePath);
File ofile=new File(targetPath);
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
//
BufferedInputStream bi=null;
BufferedOutputStream bo=null;
try {
fileInputStream=new FileInputStream(ifile);
fileOutputStream=new FileOutputStream(ofile);
bi=new BufferedInputStream(fileInputStream);
bo=new BufferedOutputStream(fileOutputStream);
//读取文件
int rel=-1;
while((rel=bi.read())!=-1){
bo.write(rel);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
fileOutputStream.close();<pre name="code" class="java"><span style="white-space:pre"> </span>bi.close();bo.close();
} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}long end = System. currentTimeMillis();long time=end-start;System.out.println(time);}}
前两者都是 一个字节一个字节的读取的,接下来一块一块的读取来进行优化
package com.insigma.chapter13;
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 java.io.InputStream;
public class FileCopyDemo {
/**
* 文件拷贝
* @param args
*/
public static void main(String[] args) {
long start = System. currentTimeMillis();
//文件源路径
String sourcePath="F:/me/2013-09-04_132030.png";
//要拷贝到的路径
String targetPath="F:/copy.png";
File ifile=new File(sourcePath);
File ofile=new File(targetPath);
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
//
BufferedInputStream bi=null;
BufferedOutputStream bo=null;
try {
fileInputStream=new FileInputStream(ifile);
fileOutputStream=new FileOutputStream(ofile);
bi=new BufferedInputStream(fileInputStream);
bo=new BufferedOutputStream(fileOutputStream);
//读取文件
/* int rel=-1;
while((rel=bi.read())!=-1){
bo.write(rel);
}*/
//要防止文件大小不是1024的倍数而被读坏
<span style="color:#ff0000;">byte[] temp=new byte[1024];
int length = 0;
while((length=bi.read(temp))!=-1){
bo.write(temp);
}</span>
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
fileOutputStream.close();bi.close();bo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System. currentTimeMillis();
long time=end-start;
System.out.println(time);
}
}