java使用IO流进行文件复制&性能比较

public class CopyFileCompare {

    /**
     * 1.普通文件流复制文件
     * 耗时:十分钟过去了。。。
     * @param source
     * @param target
     */
    public static void copyByFIS(String source, String target){
        try(
                FileInputStream fis = new FileInputStream(source);
                FileOutputStream fos = new FileOutputStream(target)
        ) {
            int b = -1;
            while ((b = fis.read()) != -1){
                fos.write(b);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 1.缓冲流复制文件
     * 耗时:文件大小:103.0MB,耗时:1.162s
     * @param source
     * @param target
     */
    public static void copyByBIS(String source, String target){
        try(
                FileInputStream fis = new FileInputStream(source);
                FileOutputStream fos = new FileOutputStream(target);
                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream( fos)
        ) {
            int b = -1;
            while ((b = bis.read()) != -1){
                bos.write(b);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 3.缓冲流使用缓冲数组复制文件
     * 耗时:文件大小:103.0MB,耗时:0.062s
     * @param source
     * @param target
     */
    public static void copyByBISBuffer(String source, String target){
        try(
                FileInputStream fis = new FileInputStream(source);
                FileOutputStream fos = new FileOutputStream(target);
                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream( fos)
        ) {
            int len = -1;
            byte[] bytes = new byte[8*1024];
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 4.NIO复制文件
     * 耗时:文件大小:103.0MB,耗时:0.072s
     * @param source
     * @param target
     */
    public static void copyByNIO(String source, String target){
        File sourceFile = new File(source);
        File targetFile = new File(target);
        try(
                FileInputStream fis = new FileInputStream(sourceFile);
                FileOutputStream fos = new FileOutputStream(targetFile);
                FileChannel inChannel = fis.getChannel();
                FileChannel outChannel = fos.getChannel()
        ) {
            ByteBuffer buf = ByteBuffer.allocate(8*1024);
            while (inChannel.read(buf) != -1) {
                buf.flip();
                while (outChannel.write(buf) != 0);
                buf.clear();
            }
            outChannel.force(true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取文件大小MB
     * @param fileName
     * @return
     */
    public static double getFileSize(String fileName){
        File file = new File(fileName);
        long length = file.length();
        double size = length / 1024 / 1024;
        int temp = (int)(size * 100);
        return temp / 100.0;
    }


    public static void main(String[] args) {
        String source = "/Users/tom/Documents/M1 软件包/zulu JDK/zulu8.64.0.19-ca-jdk8.0.345-macosx_aarch64.dmg";
        String target = "/Users/tom/Documents/M1 软件包/zulu JDK/zulu8_copy.dmg";
        long start = System.currentTimeMillis();
        // 复制文件
        copyByBISBuffer(source,target);
        long end = System.currentTimeMillis();
        double size = getFileSize(source);
        double time = (end - start)/1000.0;
        System.out.println("复制完成,文件大小:" + size + "MB,耗时:" + time + "s");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值