3种文件复制

博客对比了使用inputStream、buffer、FileChannel的read和write方法,以及FileChannel的transferTo方法读写3.82G文件的耗时。前几种方法耗时基本一致,约5秒,而transferTo方法仅需2秒左右,效率是其他方法两倍多,且测试在SSD电脑上进行,机械硬盘速度会慢很多。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Copy {

    public static void main(String[] args) throws IOException {
        //该文件3.82G
        String readPath = "D://Win10-x64.GHO";
        String writePath = "E://Win10-x64.GHO";
//        copyFileByNIO(readPath, writePath);
//        copyFileByFileInputString(readPath, writePath);
//        copyFileByBuffer(readPath, writePath);
        copyFile(readPath, writePath);

    }

    private static void copyFileByNIO(String readPath, String writePath) throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream(readPath);
        FileOutputStream fos = new FileOutputStream(writePath);
        FileChannel readChanel = fis.getChannel();
        FileChannel writeChanel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(8192);
        while (true) {
            buffer.clear();
            int length = readChanel.read(buffer);
            if (length == -1) {
                break;
            }
            buffer.flip();
            writeChanel.write(buffer);
        }
        readChanel.close();
        writeChanel.close();
        System.out.println( "NIO  :" + (System.currentTimeMillis() - start));
    }
    private static void copyFileByFileInputString(String readPath, String writePath) throws IOException {
        long start = System.currentTimeMillis();
        byte[] b = new byte[8192];
        FileInputStream dis = new FileInputStream(readPath);
        FileOutputStream dos = new FileOutputStream(writePath);
        while((dis.read(b))!=-1){
            dos.write(b);
        }
        dis.close();
        dos.close();
        System.out.println( "FileInputString    :" + (System.currentTimeMillis() - start));
    }
    private static void copyFileByBuffer(String readPath, String writePath) throws IOException {
        long start = System.currentTimeMillis();
        BufferedInputStream dis =  new BufferedInputStream(new FileInputStream(readPath) );
        BufferedOutputStream dos =  new BufferedOutputStream(new FileOutputStream(writePath) );
        byte[] b = new byte[8192];
        while((dis.read(b))!=-1){
            dos.write(b);
        }
        dis.close();
        dos.close();
        System.out.println("Buffer  :" +( System.currentTimeMillis() - start));
    }


    public static void  copyFile(String readPath, String writePath) throws IOException {
        long start = System.currentTimeMillis();
        File source = new File(readPath);
        File target = new File(writePath);
        FileInputStream is = new FileInputStream(source);
        FileChannel in = is.getChannel();
        FileOutputStream os = new FileOutputStream(target);
        FileChannel out = os.getChannel();
        long position = 0;
        long size = in.size();

        while (position <= size) {
            long transfer = in.transferTo(position, size, out);
            if (transfer <= 0) {
                break;
            }
            position += transfer;
        }
        System.out.println(System.currentTimeMillis() - start);
    }


}

分别使用inputStream ,buffer,FileChannel 进行read,write 。读写3.82G的文件,耗时基本一致,5秒左右。
使用FileChannel 的transferTo 方法,耗时 2秒左右。
我用的测试电脑是使用的ssd,速度较快,如果使用机械硬盘速度会慢很多。
FileChannel 的transferTo 方法是其他方法效率两倍多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值