java多种方式实现1G文件复制
2017-05-23 19:51
429 查看
比较复制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];//
4000
36128ms
while((len=br.read(ch))!=-1){
fr.write(ch);
}
long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}
}
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];//
4000
36128ms
while((len=br.read(ch))!=-1){
fr.write(ch);
}
long end=System.currentTimeMillis();
System.out.println("用时:"+(end-start)+"毫秒");
br.close();
fr.close();
}
}
相关文章推荐
- Java实现文件复制的四种方式
- java 实现文件复制的两种方式
- java实现简单文件复制(文件上传)以及springMVC中上传文件方式
- Java实现文件复制的四种方式
- Java实现对于文件的多种操作,复制、删除、重命名、剪切文件、统计指定类型文件数等10个功能
- java 实现文件复制的两种方式
- java 实现文件复制的两种方式
- Java实现文件复制的两种方式
- java实现FTP远程文件移动(重命名、复制、拷贝)
- java中多种方式读文件
- java中多种方式读文件,追加文件内容,对文件的各种操作
- 实现文件复制、剪切、删除操作 Java
- java实现文件和文件夹的复制
- Java 多种文件读写方式
- 【Java】运用多种方式读取文件内容,包括按字节、字符和按行为单位读取文件内容
- java中实现复制文件和文件夹
- Java中实现复制文件或文件夹
- JAVA学习提高之----Java的多种方式读写Properties文件
- java实现限时复制文件,若超时则删除所复制的文件
- 生产者/消费者问题的多种Java实现方式