第一種方法:古老的方式
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ù)分別是原始文件,和拷貝的目的文件.這里不做過多介紹.
實現(xiàn)方法很簡單,分別對2個文件構(gòu)建輸入輸出流,并且使用一個字節(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++;
}
}
實現(xiàn)方法:在第一種實現(xiàn)方法基礎(chǔ)上對輸入輸出流獲得其管道,然后分批次的從f1的管道中像f2的管道中輸入數(shù)據(jù)每次輸入的數(shù)據(jù)最大為2MB
方法3:內(nèi)存文件景象寫(讀文件沒有使用文件景象,有興趣的可以回去試試,,我就不試了,估計會更快)
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){
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();
}
}
實現(xiàn)方法:跟傷2個例子不一樣,這里寫文件流沒有使用管道而是使用內(nèi)存文件映射(假設(shè)文件f2在內(nèi)存中).在循環(huán)中從f1的管道中讀取數(shù)據(jù)到字節(jié)數(shù)組里,然后在像內(nèi)存映射的f2文件中寫數(shù)據(jù).
第4種方法:管道對管道
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){
length=(int)(inC.size()-inC.position());
}else
length=2097152;
b=ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}
這里實現(xiàn)方式與第3種實現(xiàn)方式很類似,不過沒有使用內(nèi)存影射。