package com.imooc.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOUtil {
public static void copyFile(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[8*1024];
int b ;
while((b = in.read(buf,0,buf.length))!=-1){
out.write(buf,0,b);
out.flush();
}
in.close();
out.close();
}
public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int c ;
while((c = bis.read())!=-1){
bos.write(c);
bos.flush();
}
bis.close();
bos.close();
}
public static void copyFileByBuffer2(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
byte[] buf = new byte[8*1024];
int b ;
while((b = bis.read(buf,0,buf.length))!=-1){
bos.write(buf,0,b);
bos.flush();
}
bis.close();
bos.close();
}
public static void copyFileByByte(File srcFile,File destFile)throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
int c ;
while((c = in.read())!=-1){
out.write(c);
out.flush();
}
in.close();
out.close();
}
}
package com.imooc.io;
import java.io.File;
import java.io.IOException;
public class IOUtilTest4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File f=new File("e:\\guanjujinqinpp.apk");
//按字节批量读取方式拷贝文件时间测试100MB以上
long start = System.currentTimeMillis();
IOUtil.copyFile(f, new File(
"e:\\test1.apk"));
long end = System.currentTimeMillis();
System.out.println("文件大小"+f.length()/1024/1024+"MB\t"+"字节批量读取方式拷贝文件所需时间"+(end - start)+"ms" );
//按批量缓冲方式读取文件拷贝文件时间测试100MB以上
start = System.currentTimeMillis();
IOUtil.copyFileByBuffer2(f, new File(
"e:\\test2.apk"));
end = System.currentTimeMillis();
System.out.println("文件大小"+f.length()/1024/1024+"MB\t"+"缓冲批量读取方式拷贝文件所需时间"+(end - start)+"ms" );
//按缓冲方式读取方式拷贝文件测试100MB以上
start = System.currentTimeMillis();
IOUtil.copyFileByBuffer(f, new File(
"e:\\test3.apk"));
end = System.currentTimeMillis();
System.out.println("文件大小"+f.length()/1024/1024+"MB\t"+"缓冲读取方式拷贝文件所需时间"+(end - start) +"ms");
//按字节读取方式拷贝文件测试100MB以上
start = System.currentTimeMillis();
IOUtil.copyFileByByte(f, new File(
"e:\\test4.apk"));
end = System.currentTimeMillis();
System.out.println("文件大小"+f.length()/1024/1024+"MB\t"+"字节读取方式拷贝文件所需时间"+(end - start)+"ms" );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过比较,可以看出批量拷贝的优势,但是在100MB左右的文件拷贝条件下,批量缓冲比批量字节并没有太大的优势。