Java 复制文件的方法

  1. 最快的方法
  2. 监测复制进度
  3. 常用的方法1
  4. 常用的方法2

原文:http://blog.csdn.net/zmyde2010/article/details/6024496

最快的方法

不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):

private static void nioTransferCopy(File source, File target) {
    FileChannel in = null;
    FileChannel out = null;
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(source);
        outStream = new FileOutputStream(target);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(inStream != null){
                inStream.close();
            }
            if(in != null){
                in.close();
            }
            if(outStream != null){
                outStream.close();
            }
            if(out != null){
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

监测复制进度

如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):

private static void nioBufferCopy(File source, File target) {
    FileChannel in = null;
    FileChannel out = null;
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(source);
        outStream = new FileOutputStream(target);
        in = inStream.getChannel();
        out = outStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        while (in.read(buffer) != -1) {
            buffer.flip();
            out.write(buffer);
            buffer.clear();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(inStream != null){
                inStream.close();
            }
            if(in != null){
                in.close();
            }
            if(outStream != null){
                outStream.close();
            }
            if(out != null){
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

常用的方法1

private static void customBufferBufferedStreamCopy(File source, File target) {
    InputStream fis = null;
    OutputStream fos = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(source));
        fos = new BufferedOutputStream(new FileOutputStream(target));
        byte[] buf = new byte[4096];
        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(fis != null){
                fis.close();
            }
            if(fos != null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

常用的方法2

private static void customBufferStreamCopy(File source, File target) {
    InputStream fis = null;
    OutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        byte[] buf = new byte[4096];
        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(fis != null){
                fis.close();
            }
            if(fos != null){
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 bin07280@qq.com

文章标题:Java 复制文件的方法

文章字数:392

本文作者:Bin

发布时间:2018-03-10, 16:07:14

最后更新:2019-08-06, 00:07:35

原始链接:http://coolview.github.io/2018/03/10/Java/Java%20%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6%E7%9A%84%E6%96%B9%E6%B3%95/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录