java多种方式实现1G文件复制

时间:2021-08-27 14:16:00

比较复制1G视频文件时间的快慢,其中byte[]数组大小可以更改,发现byte[512]时复制时间比byte[1024]明显更快,可以选择适合的byte数组大小实现更快的速度

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
public class CopyFile1{
public static void main(String[] args)throws IOException{
mappedBuffer();
//fileCopy("Room.mp4","Room2.mp4");
//fileCopy2("Room.mp4","Room3.mp4");
//bufferedReader1("Room.mp4","Room4.mp4");
//bufferedReader2("Room.mp4","Room5.mp4");
}

//FileChannel方法
public static void mappedBuffer() throws IOException{
long start=System.currentTimeMillis();
FileChannel read =new FileInputStream("Room.mp4").getChannel();
FileChannel writer=new RandomAccessFile("Room1.mp4","rw").getChannel();
long i=0;
long size=read.size()/30;
ByteBuffer bb,cc=null;
while(i<read.size()&&(read.size()-i)>size){
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();
long end=System.currentTimeMillis();

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

//FileInputStream方法
public static void fileCopy(String srcFile,String tarFile)throws IOException{
long start=System.currentTimeMillis();
System.out.println("初始时间:"+start);
File f=new File(srcFile);
File f1=new File(tarFile);
FileInputStream fis=null;


FileOutputStream fos=null;

fis=new FileInputStream(f);
fos=new FileOutputStream(f1);
//int t=(int)f.length();
int len=0;

//byte[] b=new byte[t];
//byte[] b=new byte[1024]; //37711毫秒
//byte[] b=new byte[512];//    15712毫秒
byte[] b=new byte[200];      //29256毫秒
//int i=fis.read(b);

while((len=fis.read(b))!=-1){
//fos.write(b);
/* int length1=fis.available();
System.out.println("长度"+length1); */
fos.write(b);
fos.flush();
}

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

if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
}



//BufferedOutputStream方法
public static void fileCopy2(String srcFile,String tarFile)throws IOException{
long start=System.currentTimeMillis();
BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream(new File(tarFile)));
BufferedInputStream fis=new BufferedInputStream(new FileInputStream(new File(srcFile)));
int len=0;
byte[] b=new byte[1024];//13652ms
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();
}
}

//BufferedReader
public static void bufferedReader1(String srcFile,String tarFile)throws IOException{
long start=System.currentTimeMillis();
BufferedReader br=new BufferedReader(new FileReader(new File(srcFile)));
BufferedWriter fr=new BufferedWriter(new FileWriter(new File(tarFile)));
int len=0;
char[] ch=new char[1024];//34701ms
while(((len=br.read(ch))!=-1)){
fr.write(ch);
}
long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}

//FileReader
public static void bufferedReader2(String srcFile,String tarFile)throws IOException{
long start=System.currentTimeMillis();
FileReader br=new FileReader(new File(srcFile));
FileWriter fr=new FileWriter(new File(tarFile));
int len=0;
char[] ch=new char[1024];//36128ms
while((len=br.read(ch))!=-1){
fr.write(ch);
}
long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}

}