Java實現復制文件或者文件夾


 拷貝一個文件的算法比較簡單,當然,可以對它進行優化,比如使用緩沖流,提高讀寫數據的效率等。

 話不多說直接上代碼

import java.io.*;

/**
 * 實現文件的拷貝
 */
public class CopyFile {

    /**
     * 復制文件夾
     *
     * @param resource 源路徑
     * @param target   目標路徑
     */
    public static void copyFolder(String resource, String target) throws Exception {

        File resourceFile = new File(resource);
        if (!resourceFile.exists()) {
            throw new Exception("源目標路徑:[" + resource + "] 不存在...");
        }
        File targetFile = new File(target);
        if (!targetFile.exists()) {
            throw new Exception("存放的目標路徑:[" + target + "] 不存在...");
        }

        // 獲取源文件夾下的文件夾或文件
        File[] resourceFiles = resourceFile.listFiles();

        for (File file : resourceFiles) {

            File file1 = new File(targetFile.getAbsolutePath() + File.separator + resourceFile.getName());
            // 復制文件
            if (file.isFile()) {
                System.out.println("文件" + file.getName());
                // 在 目標文件夾(B) 中 新建 源文件夾(A),然后將文件復制到 A 中
                // 這樣 在 B 中 就存在 A
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                File targetFile1 = new File(file1.getAbsolutePath() + File.separator + file.getName());
                copyFile(file, targetFile1);
            }
            // 復制文件夾
            if (file.isDirectory()) {// 復制源文件夾
                String dir1 = file.getAbsolutePath();
                // 目的文件夾
                String dir2 = file1.getAbsolutePath();
                copyFolder(dir1, dir2);
            }
        }

    }

    /**
     * 復制文件
     *
     * @param resource
     * @param target
     */
    public static void copyFile(File resource, File target) throws Exception {
        // 輸入流 --> 從一個目標讀取數據
        // 輸出流 --> 向一個目標寫入數據

        long start = System.currentTimeMillis();

        // 文件輸入流並進行緩沖
        FileInputStream inputStream = new FileInputStream(resource);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

        // 文件輸出流並進行緩沖
        FileOutputStream outputStream = new FileOutputStream(target);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        // 緩沖數組
        // 大文件 可將 1024 * 2 改大一些,但是 並不是越大就越快
        byte[] bytes = new byte[1024 * 2];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, len);
        }
        // 刷新輸出緩沖流
        bufferedOutputStream.flush();
        //關閉流
        bufferedInputStream.close();
        bufferedOutputStream.close();
        inputStream.close();
        outputStream.close();

        long end = System.currentTimeMillis();

        System.out.println("耗時:" + (end - start) / 1000 + " s");

    }


    // 使用示例
    public static void main(String[] args) {

        String rootPath = LoggerUtil.getJarRootPath();
        // rootPath = "E:\MyProject\student\target\classes";

        System.out.println("--------------------------------復制文件-------------------------------------------");

        File f1 = new File("D:\\GHO\\Windows10企業版.iso");
        // 目標文件
        File f2 = new File("F:\\logs\\" + "win10.iso");

        try {
            // 這個 win10系統 大概是 3.50G 的 復制過程 花了 156 秒 == 2 分6 秒
            copyFile(f1, f2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("--------------------------------復制文件夾-------------------------------------------");

        String resource = rootPath + "logs" + File.separator + "job1234";
        String target = rootPath + "logs" + File.separator + "job123";
        try {
            copyFolder(resource, target);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM