首页 > 编程语言 >Java实现文件拷贝的4种方法.

Java实现文件拷贝的4种方法.

时间:2023-05-16 12:32:19浏览次数:41  
标签:文件 f2 Java f1 close new 拷贝 out inC



第一种方法:古老的方式

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种方法:使用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++;
        }
    }




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

标签:文件,f2,Java,f1,close,new,拷贝,out,inC
From: https://blog.51cto.com/u_3871599/6283433

相关文章

  • tomcat startup.bat配置JAVA_HOME
    使用zip的tomcat包,不改变系统的环境变量JAVA_HOME的情况下启动:我用的是tomcat6.0.16在tomcat内部修改JAVA_HOME:bin/catalina.bat中头部添加setJAVA_HOME="D:\jdk\jdk1.6"估计在bin/startup.bat添加也可以,没有测试。要保留startup.bat运行的输出日志():修改startup.ba......
  • java实现zip与unzip
    jdk提供了Zip相关的类方便的实现压缩和解压缩。使用方法很简单。下边分别是压缩和解压缩的简单事例1,压缩的importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOut......
  • 使用java mail(jmail)通过gmail的stmp发送邮件:SSL方式
    [url]http://stackoverflow.com/questions/1990454/using-javamail-to-connect-to-gmail-smtp-server-ignores-specified-port-and-tries[/url]Transporttransport=session.getTransport("smtps");transport.connect(smtp_host,smtp_por......
  • 【Java】定时器CronExpression参数规则
    参考:定时器CronExpression配置说明详解-心和梦的方向-博客园(cnblogs.com) 项目中经常要使用到定时器,其中CronExpression配置非常重要。现在就配置说明详细解说一下:CronExpression表达式是由6个必需字段(秒、分、时、日、月、周)和一个可选字段(年)通过空格组成。cronExpres......
  • 12槽2极有刷直流电机工程文件及性能波形
    12槽2极有刷直流电机工程文件及性能波形ID:49199662468470218......
  • java基础(02)
    02-Java基础语法1.注释​ 注释是对代码的解释和说明文字。Java中的注释分为三种:单行注释://这是单行注释文字多行注释:/*这是多行注释文字这是多行注释文字这是多行注释文字*/注意:多行注释不能嵌套使用。文档注释:/**这是多行注释文字这是多行注释文字......
  • 传输文件测试一下群晖2.5G网卡性能表现如何,实操验证
    各位观众好,欢迎来到黑菌的博客网站,淘宝同名,欢迎咨询!1、A文件所在的机器是N5105的群晖上B机器也安装了群晖,接了一个2.5G的网卡2、使用同步软件CloudSync,传输远程文件夹ESXI中的文件到本地的FTP文件夹 3、效果看图偶有波动是文件有大小,不是一份文件基本上稳定在260上下......
  • Java 中 ArrayList 和 LinkedList 有什么区别
    在Java中,ArrayList和LinkedList是两种常见的集合类。它们都实现了List接口,提供了类似数组的功能,可以存储任意类型的对象。虽然它们都可以实现相同的功能,但是它们的底层实现方式有所不同,因此在性能和用途上也存在一些差异。ArrayListArrayList是一个基于数组实现的动态数组,它可......
  • Linux 文件上传下载的几种方式
    虚拟终端软件中的rz和sz我们使用虚拟终端软件,如Xshell、SecureCRT或PuTTY来连接远程服务器后,可以使用rz或sz来上传下载文件rz命令使用rz命令可以上传本地文件到远程服务器。运行该命令会弹出一个文件选择窗口,从本地选择文件上传到Linux服务器sz命令将选定的......
  • Java中两个字符串进行大小比较
    一:大小比较:使用String.compareTo方法如果需要忽略大小写,使用compareToIgnoreCasecompareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)1、如果字符串相等返回值02、如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值)(负值表示前字符......