日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java文件拷贝_Java实现文件拷贝的4种方法

發(fā)布時(shí)間:2023/12/10 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java文件拷贝_Java实现文件拷贝的4种方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一種方法:古老的方式

public?static?long?forJava(File?f1,File?f2)?throws?Exception{

long?time=new?Date().getTime();

int?length=2097152;

FileInputStream?in=new?FileInputStream(f1);

FileOutputStream?out=new?FileOutputStream(f2);

byte[]?buffer=new?byte[length];

while(true){

int?ins=in.read(buffer);

if(ins==-1){

in.close();

out.flush();

out.close();

return?new?Date().getTime()-time;

}else

out.write(buffer,0,ins);

}

}

方法的2參數(shù)分別是原始文件,和拷貝的目的文件.這里不做過多介紹.

實(shí)現(xiàn)方法很簡(jiǎn)單,分別對(duì)2個(gè)文件構(gòu)建輸入輸出流,并且使用一個(gè)字節(jié)數(shù)組作為我們內(nèi)存的緩存器, 然后使用流從f1 中讀出數(shù)據(jù)到緩存里,在將緩存數(shù)據(jù)寫到f2里面去.這里的緩存是2MB的字節(jié)數(shù)組

第2種方法:使用NIO中的管道到管道傳輸

public?static?long?forTransfer(File?f1,File?f2)?throws?Exception{

long?time=new?Date().getTime();

int?length=2097152;

FileInputStream?in=new?FileInputStream(f1);

FileOutputStream?out=new?FileOutputStream(f2);

FileChannel?inC=in.getChannel();

FileChannel?outC=out.getChannel();

int?i=0;

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.close();

return?new?Date().getTime()-time;

}

if((inC.size()-inC.position())<20971520)

length=(int)(inC.size()-inC.position());

else

length=20971520;

inC.transferTo(inC.position(),length,outC);

inC.position(inC.position()+length);

i++;

}

}

實(shí)現(xiàn)方法:在第一種實(shí)現(xiàn)方法基礎(chǔ)上對(duì)輸入輸出流獲得其管道,然后分批次的從f1的管道中像f2的管道中輸入數(shù)據(jù)每次輸入的數(shù)據(jù)最大為2MB

方法3:內(nèi)存文件景象寫(讀文件沒有使用文件景象,有興趣的可以回去試試,,我就不試了,估計(jì)會(huì)更快)

public?static?long?forImage(File?f1,File?f2)?throws?Exception{

long?time=new?Date().getTime();

int?length=2097152;

FileInputStream?in=new?FileInputStream(f1);

RandomAccessFile?out=new?RandomAccessFile(f2,"rw");

FileChannel?inC=in.getChannel();

MappedByteBuffer?outC=null;

MappedByteBuffer?inbuffer=null;

byte[]?b=new?byte[length];

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.force();

out.close();

return?new?Date().getTime()-time;

}

if((inC.size()-inC.position())

length=(int)(inC.size()-inC.position());

}else{

length=20971520;

}

b=new?byte[length];

inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);

inbuffer.load();

inbuffer.get(b);

outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);

inC.position(b.length+inC.position());

outC.put(b);

outC.force();

}

}

實(shí)現(xiàn)方法:跟傷2個(gè)例子不一樣,這里寫文件流沒有使用管道而是使用內(nèi)存文件映射(假設(shè)文件f2在內(nèi)存中).在循環(huán)中從f1的管道中讀取數(shù)據(jù)到字節(jié)數(shù)組里,然后在像內(nèi)存映射的f2文件中寫數(shù)據(jù).

第4種方法:管道對(duì)管道

public?static?long?forChannel(File?f1,File?f2)?throws?Exception{

long?time=new?Date().getTime();

int?length=2097152;

FileInputStream?in=new?FileInputStream(f1);

FileOutputStream?out=new?FileOutputStream(f2);

FileChannel?inC=in.getChannel();

FileChannel?outC=out.getChannel();

ByteBuffer?b=null;

while(true){

if(inC.position()==inC.size()){

inC.close();

outC.close();

return?new?Date().getTime()-time;

}

if((inC.size()-inC.position())

length=(int)(inC.size()-inC.position());

}else

length=2097152;

b=ByteBuffer.allocateDirect(length);

inC.read(b);

b.flip();

outC.write(b);

outC.force(false);

}

}

這里實(shí)現(xiàn)方式與第3種實(shí)現(xiàn)方式很類似,不過沒有使用內(nèi)存影射.

下面是對(duì)49.3MB的文件進(jìn)行拷貝的測(cè)試時(shí)間(毫秒)

Start Copy File... ?file size:50290KB

CopyFile:b1.rmvb mode:forChannel ?RunTime:3203

CopyFile:b1.rmvb mode:forImage ?RunTime:3328

CopyFile:b1.rmvb mode:forJava ?RunTime:2172

CopyFile:b1.rmvb mode:forTransfer RunTime:1406

End Copy File!

總結(jié)

以上是生活随笔為你收集整理的java文件拷贝_Java实现文件拷贝的4种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。