大文件拷贝方法

简介: 文件拷贝方法


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;
}

}

相关文章
|
5月前
|
Linux 编译器
目录文件篇
目录文件篇
|
10月前
|
存储 监控 安全
11.1 文件拷贝移动与删除
在编程中,针对磁盘与目录的操作也是非常重要的,本章将重点介绍如何实现针对文件目录与磁盘的操作方法,其中包括了删除文件,文件拷贝,文件读写,目录遍历输出,遍历磁盘容量信息,磁盘格式化,输出分区表数据,监控目录变化等。
38 0
|
12月前
45 # 实现文件拷贝功能
45 # 实现文件拷贝功能
44 0
|
12月前
文件复制操作
文件复制操作
47 0
|
Java Apache
java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹
java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹
763 0
C#编程-34:拷贝整个文件夹
C#编程-34:拷贝整个文件夹
|
Java 开发者
文件拷贝 |学习笔记
快速学习 文件拷贝
101 0
|
存储 分布式计算 Hadoop