Java基础篇--文件拷贝

本文介绍了六种不同的文件拷贝方法,包括逐字节读写、字节数组拷贝、带缓冲区的字节数组拷贝、使用通道进行读写、内存映射文件拷贝以及通道直接传输。通过实际测试比较了各种方法的执行速度。

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

文件拷贝

使用Io操作进行文件拷贝
想要更好的体验,我们需要找到一个.mp4文件作为目标文件
这里一共有六种实现方法,大家可以试一试,看看哪一个更快。

package com.io1;

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

public class TestFileCopy {

    public static void main(String[] args) throws  Exception{
            fileCopy2("F:\\a.mp4","F:\\b.mp4");
        fileCopy3("F:\\a.mp4","F:\\c.mp4");
        fileCopy4("F:\\a.mp4","F:\\d.mp4");
        fileCopy5("F:\\a.mp4","F:\\e.mp4");
        fileCopy6("F:\\a.mp4","F:\\f.mp4");
}

//方法一
    static void fileCopy1(String srcName,String destName) throws Exception{//一个字节一个字节读写,太慢,有时间可以等,反正被我淘汰。
        FileInputStream fis = new FileInputStream(srcName);
        FileOutputStream fos = new FileOutputStream(destName);
        while(true){
            int a = fis.read();
            if (a == -1) break;
            fos.write(a);
        }
        fis.close();
        fos.close();
    }

//方法二
    static void fileCopy2(String srcName,String destName) throws Exception{//一次拷贝一个字节数组
        long t1 = System.nanoTime();

        FileInputStream fis = new FileInputStream(srcName);
        FileOutputStream fos = new FileOutputStream(destName);
        byte[] bs = new byte[1024];
        while(true){
            int len = fis.read(bs);
            if (len == -1) break;
            // fos.write(bs);//如果用这个请对比两个文件属性,查看字节数,发现不一样
            fos.write(bs, 0 , len);
        }
        fis.close();
        fos.close();

        long t2 = System.nanoTime();
        System.out.println((t2-t1)/1E9);
    }
//方法三
    static void fileCopy3(String srcName,String destName) throws Exception{//不仅拷字节数组,还加了一个缓冲区
        long t1 = System.nanoTime();

        FileInputStream fis = new FileInputStream(srcName);
        BufferedInputStream in = new BufferedInputStream(fis);

        FileOutputStream fos = new FileOutputStream(destName);
        BufferedOutputStream out = new BufferedOutputStream(fos);

        byte[] bs = new byte[1024];
        while(true){
            int len = in.read(bs);
            if (len == -1) break;
            out.write(bs, 0 , len);
        }

        in.close();
        out.close();

        long t2 = System.nanoTime();
        System.out.println((t2-t1)/1E9);
    }
//使用通道解决
//方法四
    static void fileCopy4(String srcName,String destName) throws Exception{
        long t1 = System.nanoTime();

        ByteBuffer buffer = ByteBuffer.allocate(1024);//ByteBuffer.allocate(容量)       ByteBuffer.wrap(byte[])
        FileInputStream fis = new FileInputStream(srcName);
        FileOutputStream fos = new FileOutputStream(destName);
        FileChannel channel1 = fis.getChannel();
        FileChannel channel2 = fos.getChannel();

        while(true){
            int a = channel1.read(buffer);
            if (a == -1) break;
            /*参考Buffer  API
            position:当前读写位置
            limit:限制
            capacity:容量
            flip():position重置为0,即从写模式到读模式
            clear(): position、limit重置为0,从读模式到写模式,
            */
            buffer.flip();//写模式->读模式
            channel2.write(buffer);
            buffer.clear();//读模式->写模式。清空,准备下一次
        }
        channel1.close();
        channel2.close();

        long t2 = System.nanoTime();
        System.out.println((t2-t1)/1E9);
    }
//方法五
    static void fileCopy5(String srcName,String destName) throws Exception{
        long t1 = System.nanoTime();

        FileInputStream fis = new FileInputStream(srcName);
        FileOutputStream fos = new FileOutputStream(destName);
        FileChannel channel1 = fis.getChannel();
        FileChannel channel2 = fos.getChannel();

        MappedByteBuffer buffer = channel1.map(FileChannel.MapMode.READ_ONLY, 0, channel1.size());
        channel2.write(buffer);

        channel1.close();
        channel2.close();

        long t2 = System.nanoTime();
        System.out.println((t2-t1)/1E9);
    }

//方法六
    static void fileCopy6(String srcName,String destName) throws Exception{
        long t1 = System.nanoTime();

        FileInputStream fis = new FileInputStream(srcName);
        FileOutputStream fos = new FileOutputStream(destName);
        FileChannel channel1 = fis.getChannel();
        FileChannel channel2 = fos.getChannel();

        channel1.transferTo(0, channel1.size(), channel2);
        channel1.close();
        channel2.close();

        long t2 = System.nanoTime();
        System.out.println((t2-t1)/1E9);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值