码迷,mamicode.com
首页 > 编程语言 > 详细

java文件复制

时间:2016-05-24 16:44:33      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

1.

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.

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++;  
     }  
 }

 

3.

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();  
     }  
 }

 

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);  
     }  
 }

 

5.

import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel;

public class TestTransfer {

public static void main(String[] args) throws Exception {

  FileInputStream fis = new FileInputStream("c:\\abc.txt");

  FileOutputStream fos = new FileOutputStream("c:\\123.txt");  

FileChannel fc1 = fis.getChannel();

  FileChannel fc2 = fos.getChannel();

  fc2.transferFrom(fc1, 0, fc1.size());

//  fc1.transferTo(0,fc1.size(),fc2);  

fc1.close();  

fc2.close();  

fis.close();  

fos.close();

}

}

java文件复制

标签:

原文地址:http://www.cnblogs.com/xianhaiyuan/p/5523765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!