大文件复制 java_java 1G大文件复制

对比几种复制方法

复制的文件是980m的txt文件

6b30c07b254b58be2e3ad673233f0de8.png

66ac8d06ad47ec18a6e6a238fc128db5.png

1、  FileChannel 方法

代码:

public static void mappedBuffer() throwsIOException{long start=System.currentTimeMillis();

FileChannel read= new FileInputStream("n2.txt").getChannel();

FileChannel writer= new RandomAccessFile("n5.txt","rw").getChannel();long i = 0;long size = read.size()/30;

ByteBuffer bb,cc= null;while(isize){

bb=read.map(FileChannel.MapMode.READ_ONLY, i, size);

cc=writer.map(FileChannel.MapMode.READ_WRITE, i, size);

cc.put(bb);

i+=size;

bb.clear();

cc.clear();

}

bb= read.map(FileChannel.MapMode.READ_ONLY, i, read.size()-i);

cc.put(bb);

bb.clear();

cc.clear();

read.close();

writer.close();long end=System.currentTimeMillis();

System.out.println("用时:"+(end-start)+"毫秒");

}

耗时:807ms

6dbb4107a73b9256cc8ddeb5fbb32969.png

使用NewIO技术复制大文件的速度最快,尤其是此方法中使用了内存映射技术,速度非常快。

2、  FileInputStream技术

public static void fileCopy(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();

FileInputStream fis= null;

FileOutputStream fos= null;

File f=newFile(srcFile);

fis= newFileInputStream(f);

fos= newFileOutputStream(tarFile);int len=0;byte[] b =new byte[t];while((len=fis.read(b))!=-1){

fos.write(b);

fos.flush();

}long end=System.currentTimeMillis();

System.out.println("用时:"+(end-start)+"毫秒");if(fis!=null){

fis.close();

}if(fos!=null){

fos.close();

}

}

耗时:

962ca86f50f617467d2684882f3a519c.png

1072ms,速度也不慢,在处理文本文档的时候传统的io技术速度并不慢,但如果处理的是图像流文件,速度比NIO技术慢很多。

3、  BufferedOutputStream

比起FileInputStream多了一层包装

public static void fileCopy2(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();

BufferedOutputStream fos= new BufferedOutputStream (new FileOutputStream(newFile (tarFile)));

BufferedInputStream fis= new BufferedInputStream (new FileInputStream(newFile (srcFile)));int len=0;byte[] b =new byte[t];while((len=fis.read(b))!=-1){

fos.write(b);

fos.flush();

}long end=System.currentTimeMillis();

System.out.println("用时:"+(end-start)+"毫秒");if(fis!=null){

fis.close();

}if(fos!=null){

fos.close();

}

}

耗时:

c61a106d6236a498ce50d7eb4058c034.png

耗时问1175ms比FileInputStream慢了100ms(此处比较的前提是缓存数组大小一致 为100000)

4、  BufferedReader

public static void bufferedReader(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();

BufferedReader br=new BufferedReader(new FileReader(newFile(srcFile)));

BufferedWriter fr=new BufferedWriter(new FileWriter(newFile(tarFile)));int len = 0;char[] ch =new char[t];while((len=br.read(ch))!=-1){

fr.write(ch);

}long end=System.currentTimeMillis();

System.out.println("用时:"+(end-start)+"毫秒");

br.close();

fr.close();

}

fd3db0f1cb17684df277bfff12ca6824.png

耗时足足达到50s,比起前几种方法简直天差地别,但此参数并非最优参数,如果改变数组大小,速度能明显提升

bf8113eea6eba7cc0a802717df50fe58.png

可比起前面的方法还是差了很远。

5、  FileReader

public static void bufferedReader2(String srcFile,String tarFile)throwsIOException{long start=System.currentTimeMillis();

FileReader br=new FileReader(newFile(srcFile));

FileWriter fr=new FileWriter(newFile(tarFile));int len = 0;char[] ch =new char[t];while((len=br.read(ch))!=-1){

fr.write(ch);

}long end=System.currentTimeMillis();

System.out.println("用时:"+(end-start)+"毫秒");

br.close();

fr.close();

}

此方法比起BufferedReader少了一层包装,速度也更快些

bed9eabe7fd7cfd2c733b34bc69e9dc6.png

经过测试发现此方法的速度受数组大小的影响程度不大

此份文档中所有的方法的参数虽不是最优,但各种方法之间的速度有明显的差距,就不再累赘逐一寻找最优参数了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值