大文件拷贝方法

简介: 文件拷贝方法


package top.my.test.case1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileCopyTest {

public static void main(String[] args) {
    // 文件大小为4.5G
    
    System.out.println(System.currentTimeMillis());
    // 1595581151315 -- 1595581253196 = 101881ms = 101s
    // copyFile(new File("D:\\xl\\big.mkv"), new File("D:\\big.mkv"));
    // 1595582378585 -- 1595582548529 = 169944ms = 169s
    // fileChannelCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
    // 1595582683903 -- 1595582805496 = 121593ms = 121s
    // fileCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
    // 1595583767345 -- 1595583897985 = 130640ms = 130s
    // filesCopy(new File("D:\\xl\\big.mkv"), new File("D:\\big2.mkv"));
    //1595584222455 -- 1595584325169 =  102714ms = 102s
    copyBigFile(new File("D:\\xl\\big.mkv"), new File("D:\\big.mkv"));
    System.out.println(System.currentTimeMillis());
}

// 单文件复制
public static boolean copyFile(File fromFile, File toFile) {
    try (FileInputStream in = new FileInputStream(fromFile); FileOutputStream os = new FileOutputStream(toFile);) {
        byte[] b = new byte[1024];
        int n = 0;
        while ((n = in.read(b)) != -1) {
            os.write(b, 0, n);
        }
        in.close();
        os.close();
        System.out.println("文件拷贝结束");
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

public static boolean filesCopy(File s, File t) {
    Path sourcePath = Paths.get(s.getAbsolutePath());
    Path destinationPath = Paths.get(t.getAbsolutePath());

    try {
        Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        return true;
    } catch (IOException e) {
        // something else went wrong
        e.printStackTrace();
    }
    return false;
}

public static boolean fileCopy(File s, File t) {
    FileInputStream fi = null;
    FileOutputStream fo = null;
    FileChannel in = null;
    FileChannel out = null;
    try {
        fi = new FileInputStream(s);
        fo = new FileOutputStream(t);
        in = fi.getChannel();// 得到对应的文件通道
        out = fo.getChannel();// 得到对应的文件通道
        long left_size = in.size();
        System.out.println("源文件大小:" + left_size / 1024 / 1024);
        long position = 0;
        while (left_size > 0) {
            long write_size = in.transferTo(position, left_size, out);
            position += write_size;
            left_size -= write_size;
        }
        // in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
        System.out.println("FileChannel文件拷贝结束");
        System.out.println("目标文件大小:" + out.size() / 1024 / 1024);
        return true;
    } catch (IOException e) {
        System.out.print("文件出现拷贝异常:{}" + e.getMessage());
        ;
    } finally {
        try {
            fi.close();
            in.close();
            fo.close();
            out.close();
        } catch (IOException e) {
            System.out.print("文件出现拷贝异常:{}" + e.getMessage());
        }

    }
    return false;
}

public static boolean fileChannelCopy(File s, File t) {
    FileChannel in = null;
    FileChannel out = null;
    RandomAccessFile fi = null;
    RandomAccessFile fo = null;
    try {
        if (!t.isFile()) {
            if (!t.createNewFile()) {
                return false;
            }
        }
        fi = new RandomAccessFile(s, "r");
        fo = new RandomAccessFile(t, "rw");
        in = fi.getChannel();// 得到对应的文件通道
        out = fo.getChannel();// 得到对应的文件通道
        long left_size = in.size();
        long position = 0;
        while (left_size > 0) {
            long write_size = in.transferTo(position, left_size, out);
            position += write_size;
            left_size -= write_size;
        }
        // in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
        System.out.println("FileChannel文件拷贝结束");
        return true;
    } catch (IOException e) {
        System.out.print("文件出现拷贝异常:{}" + e.getMessage());
        ;
    } finally {
        try {
            fi.close();
            in.close();
            fo.close();
            out.close();
        } catch (IOException e) {
            System.out.print("文件出现拷贝异常:{}" + e.getMessage());
        }

    }
    return false;
}

public static boolean copyBigFile(File s, File t) {
    FileInputStream fi = null;
    FileOutputStream fo = null;
    FileChannel in = null;
    FileChannel out = null;
    ByteBuffer buffer = ByteBuffer.allocate(10 * 1024);
    try {
        fi = new FileInputStream(s);
        fo = new FileOutputStream(t);
        in = fi.getChannel();// 得到对应的文件通道
        out = fo.getChannel();// 得到对应的文件通道
        while (true) {
            int read = in.read(buffer);
            if (read == -1)
                break;
            buffer.flip();
            out.write(buffer);
            buffer.clear();
        }
        System.out.println("ByteBuffer文件拷贝结束");
        return true;
    } catch (IOException e) {
        System.err.print("文件出现拷贝异常:{}" + e.getMessage());
    } finally {
        try {
            fi.close();
            in.close();
            fo.close();
            out.close();
        } catch (IOException e) {
            System.err.print("文件出现拷贝异常:{}" + e.getMessage());
        }

    }
    return false;
}

}

相关文章
|
虚拟化 Docker Windows
Docker - Win10 Hyper-V 和 VirtualBox 冲突的问题
Docker - Win10 Hyper-V 和 VirtualBox 冲突的问题
1250 0
|
SQL Kubernetes 关系型数据库
如何一键安装部署PolarDB-X
《PolarDB-X 动手实践》系列第一期,体验如何一键安装部署PolarDB-X。
|
存储 Java 开发工具
解决码云Gitee上传文件大小受限的问题
解决码云Gitee上传文件大小受限的问题
1694 0
|
安全 Cloud Native Linux
CMake Install:深度解析与实践(二)
CMake Install:深度解析与实践
465 0
python 找到并去除文本中的全部链接
这篇文章提供了一个使用Python正则表达式找到并删除文本中所有链接的代码示例。
|
10月前
|
人工智能 自然语言处理 测试技术
通义灵码上新推理模型,快来体验数学编程双冠王 Qwen2.5-Max
近日,通义灵码上新模型选择功能,除新增 DeepSeek 满血版 V3 和 R1 外,Qwen2.5-Max 也正式上线,它使用了超过 20 万亿 token 的预训练数据及精心设计的后训练方案进行训练。
|
数据可视化 数据挖掘 Docker
Docker Desktop 安装 ClickHouse 超级简单教程
Docker Desktop 安装 ClickHouse 超级简单教程
874 1
|
Ubuntu 关系型数据库 应用服务中间件
在Ubuntu 18.04上安装和配置pgAdmin 4服务器模式的方法
在Ubuntu 18.04上安装和配置pgAdmin 4服务器模式的方法
453 0
|
机器学习/深度学习 算法 安全
量子计算编程语言:面向未来的开发工具
【10月更文挑战第30天】量子计算编程语言是基于量子力学原理的新型开发工具,支持量子态操作和量子算法设计。从20世纪80年代Richard Feynman提出概念,到QCL、Quipper、Q#和Quil等语言的诞生,量子计算编程语言已逐步成熟。它们在量子模拟、量子算法、量子通信和量子机器学习等领域展现出广泛应用前景,未来将随着量子计算技术的发展而进一步壮大。
|
运维 Kubernetes Devops
平台工程:它是什么?谁来做?怎么做?
大家可能听说过平台工程,这是一个新术语,它为开发和 DevOps 领域中本已拥挤的角色集合增添了新内容。 在这篇文章中,我们将介绍平台工程、它与 DevOps 的区别以及为什么你可能考虑采用平台工程以及谁需要拥有平台工程的能力。