最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java?IO?API實現(xiàn)文件復(fù)制工具

 更新時間:2026年06月15日 09:24:51   作者:Cache技術(shù)分享  
這篇文章主要為大家詳細介紹了如何基于Java?IO?API實現(xiàn)文件復(fù)制工具,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

 Java IO API - Java 文件復(fù)制工具

這是一個用 Java 編寫的遞歸文件復(fù)制工具,模仿了文件系統(tǒng)復(fù)制的操作。它從源目錄復(fù)制文件到目標目錄,并支持指定復(fù)制的最大目錄層級(-depth)。

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.EnumSet;
import java.util.stream.Stream;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.SKIP_SUBTREE;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
/**
 * Sample code that copies files recursively 
 * from a source directory to a destination folder.
 * The maximum number of directory levels to copy 
 * is specified after -depth.
 * The number of files copied is printed
 * to standard out.
 * You can execute the application using:
 * @code java Copy . new -depth 4
 */
public class Copy {
    /**
     * A {@code FileVisitor} that finds
     * all files that match the
     * specified pattern.
     */
    public static class Replicator
            extends SimpleFileVisitor<Path> {
        Path source;
        Path destination;
        public Replicator(Path source, Path destination) {
            this.source = source;
            this.destination = destination;
        }
        // Prints the total number of
        // files copied to standard out.
        void done() throws IOException {
            try (Stream<Path> path = Files.list(Paths.get(destination.toUri()))) {
                System.out.println("Number of files copied: "
                        + path.filter(p -> p.toFile().isFile()).count());
            }
        }
        // Copy a file in destination
        @Override
        public FileVisitResult visitFile(Path file,
                                         BasicFileAttributes attrs) {
            System.out.println("Copy file: " + file);
            Path newFile = destination.resolve(source.relativize(file));
            try{
                Files.copy(file,newFile);
            }
            catch (IOException ioException){
                //log it and move
            }
            return CONTINUE;
        }
        // Invoke copy of a directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir,
                                                 BasicFileAttributes attrs) {
            System.out.println("Copy directory: " + dir);
            Path targetDir = destination.resolve(source.relativize(dir));
            try {
                Files.copy(dir, targetDir, REPLACE_EXISTING, COPY_ATTRIBUTES);
            } catch (IOException e) {
                System.err.println("Unable to create " + targetDir + " [" + e + "]");
                return SKIP_SUBTREE;
            }
            return CONTINUE;
        }
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc == null) {
                Path destination = this.destination.resolve(source.relativize(dir));
                try {
                    FileTime time = Files.getLastModifiedTime(dir);
                    Files.setLastModifiedTime(destination, time);
                } catch (IOException e) {
                    System.err.println("Unable to copy all attributes to: " + destination + " [" + e + "]");
                }
            } else {
                throw exc;
            }
            return CONTINUE;
        }
        @Override
        public FileVisitResult visitFileFailed(Path file,
                                               IOException exc) {
            if (exc instanceof FileSystemLoopException) {
                System.err.println("cycle detected: " + file);
            } else {
                System.err.format("Unable to copy:" + " %s: %s%n", 
                        file, exc);
            }
            return CONTINUE;
        }
    }
    static void usage() {
        System.err.println("java Copy <source> <destination>" +
                " -depth \"<max_level_dir>\"");
        System.exit(-1);
    }
    public static void main(String[] args)
            throws IOException {
        if (args.length < 4 || !args[2].equals("-depth"))
            usage();
        Path source = Paths.get(args[0]);
        Path destination = Paths.get(args[1]);
        int depth = Integer.parseInt(args[3]);
        Replicator walk = new Replicator(source, destination);
        EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        Files.walkFileTree(source, opts, depth, walk);
        walk.done();
    }
}

功能簡介

該程序會:

  • 遞歸地將源目錄中的文件復(fù)制到目標目錄。
  • 允許通過 -depth 參數(shù)指定最大目錄層級。
  • 在程序結(jié)束時,打印成功復(fù)制的文件數(shù)。

例如:

$ java Copy . new -depth 4

會把當(dāng)前目錄及其子目錄(最多 4 層)中的文件復(fù)制到 new 目錄。

示例代碼結(jié)構(gòu)詳解

main()方法:程序入口

public static void main(String[] args) throws IOException {
    if (args.length < 4 || !args[2].equals("-depth"))
        usage();

    Path source = Paths.get(args[0]);
    Path destination = Paths.get(args[1]);
    int depth = Integer.parseInt(args[3]);

    Replicator walk = new Replicator(source, destination);
    EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    Files.walkFileTree(source, opts, depth, walk);
    walk.done();
}
  • 輸入?yún)?shù)解析:首先檢查輸入是否符合規(guī)范,若不符合則調(diào)用 usage() 打印使用說明。
  • 路徑和深度解析:從命令行參數(shù)中提取源目錄、目標目錄和最大目錄深度。
  • 文件遍歷:使用 Files.walkFileTree() 遞歸遍歷源目錄,進行復(fù)制操作。

Replicator類:文件復(fù)制核心邏輯

public static class Replicator extends SimpleFileVisitor<Path> {

該類繼承 SimpleFileVisitor<Path> 并重寫了幾個關(guān)鍵方法來執(zhí)行復(fù)制操作。

構(gòu)造方法:初始化源路徑與目標路徑

public Replicator(Path source, Path destination) {
    this.source = source;
    this.destination = destination;
}
  • source:源目錄路徑;
  • destination:目標目錄路徑。

visitFile()方法:復(fù)制文件

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
    System.out.println("Copy file: " + file);
    Path newFile = destination.resolve(source.relativize(file));
    try {
        Files.copy(file, newFile);
    } catch (IOException ioException) {
        // 錯誤日志處理
    }
    return CONTINUE;
}
  • 使用 Files.copy() 復(fù)制文件到目標路徑。
  • source.relativize(file) 計算文件的相對路徑,以保持源文件夾結(jié)構(gòu)。
  • 如果復(fù)制失敗,會捕獲并記錄異常,但繼續(xù)執(zhí)行。

preVisitDirectory()方法:復(fù)制目錄

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
    System.out.println("Copy directory: " + dir);
    Path targetDir = destination.resolve(source.relativize(dir));
    try {
        Files.copy(dir, targetDir, REPLACE_EXISTING, COPY_ATTRIBUTES);
    } catch (IOException e) {
        System.err.println("Unable to create " + targetDir + " [" + e + "]");
        return SKIP_SUBTREE;
    }
    return CONTINUE;
}
  • Files.copy() 會復(fù)制目錄及其屬性。
  • 如果目錄已存在并且指定了 REPLACE_EXISTING,它將覆蓋該目錄。
  • 如果復(fù)制失敗,打印錯誤信息并跳過該子目錄(使用 SKIP_SUBTREE)。

postVisitDirectory()方法:復(fù)制目錄屬性

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    if (exc == null) {
        Path destination = this.destination.resolve(source.relativize(dir));
        try {
            FileTime time = Files.getLastModifiedTime(dir);
            Files.setLastModifiedTime(destination, time);
        } catch (IOException e) {
            System.err.println("Unable to copy all attributes to: " + destination + " [" + e + "]");
        }
    } else {
        throw exc;
    }
    return CONTINUE;
}
  • 在目錄復(fù)制后,復(fù)制目錄的最后修改時間等屬性。
  • 如果復(fù)制失敗,打印錯誤信息。

visitFileFailed()方法:錯誤處理

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
    if (exc instanceof FileSystemLoopException) {
        System.err.println("Cycle detected: " + file);
    } else {
        System.err.format("Unable to copy: %s: %s%n", file, exc);
    }
    return CONTINUE;
}
  • 處理訪問失敗的文件。
  • 如果檢測到文件系統(tǒng)循環(huán)(符號鏈接等),打印循環(huán)錯誤信息。

done()方法:統(tǒng)計已復(fù)制文件數(shù)

void done() throws IOException {
    try (Stream<Path> path = Files.list(Paths.get(destination.toUri()))) {
        System.out.println("Number of files copied: "
                + path.filter(p -> p.toFile().isFile()).count());
    }
}

在文件復(fù)制完成后,統(tǒng)計并打印成功復(fù)制的文件數(shù)。

usage()方法:程序使用說明

static void usage() {
    System.err.println("java Copy <source> <destination> -depth \"<max_level_dir>\"");
    System.exit(-1);
}

如果輸入?yún)?shù)無效,打印使用說明并退出程序。

擴展思路與練習(xí)建議

擴展功能實現(xiàn)方法
復(fù)制文件時跳過某些類型visitFile() 方法中加入文件類型判斷(如跳過 .txt 文件)
并行復(fù)制大文件夾使用 ExecutorService 執(zhí)行并行文件復(fù)制
復(fù)制過程中顯示進度條使用 System.out.print 打印進度信息
按文件類型篩選復(fù)制visitFile() 中加入文件類型過濾,例如只復(fù)制 .java 文件
備份和版本控制visitFile() 中實現(xiàn)版本號控制,避免覆蓋舊版本

錯誤處理建議

  • IOException 提供更加詳細的日志,例如記錄文件復(fù)制失敗的具體原因,或者保存失敗文件列表。
  • visitFileFailed() 方法添加更多的錯誤類型處理,如權(quán)限不足等。

小結(jié)

這個 Copy 示例演示了如何使用 Java 的 nio 包進行遞歸文件復(fù)制。通過自定義 FileVisitor 類,你可以實現(xiàn)高效且靈活的文件操作。這個工具可以被擴展和定制化,滿足各種實際項目中的需求。

到此這篇關(guān)于基于Java IO API實現(xiàn)文件查找和復(fù)制工具的文章就介紹到這了,更多相關(guān)Java文件查找和復(fù)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring容器中添加bean的5種方式

    Spring容器中添加bean的5種方式

    我們知道平時在開發(fā)中使用Spring的時候,都是將對象交由Spring去管理,那么將一個對象加入到Spring容器中,有哪些方式呢,感興趣的可以了解一下
    2021-07-07
  • idea導(dǎo)入jar包的詳細圖文教程

    idea導(dǎo)入jar包的詳細圖文教程

    這篇文章主要給大家介紹了關(guān)于idea導(dǎo)入jar包的詳細圖文教程,文中通過圖文將導(dǎo)入的步驟介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-03-03
  • IntelliJ IDEA同步代碼時版本沖突而產(chǎn)生出的incoming partial文件問題的解決辦法

    IntelliJ IDEA同步代碼時版本沖突而產(chǎn)生出的incoming partial文件問題的解決辦法

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA同步代碼時版本沖突而產(chǎn)生出的incoming partial文件問題的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • 一文帶你徹底了解Java8中的Lambda,函數(shù)式接口和Stream

    一文帶你徹底了解Java8中的Lambda,函數(shù)式接口和Stream

    這篇文章主要為大家詳細介紹了解Java8中的Lambda,函數(shù)式接口和Stream的用法和原理,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • Java如何實現(xiàn)登錄token令牌

    Java如何實現(xiàn)登錄token令牌

    這篇文章主要介紹了Java如何實現(xiàn)登錄token令牌,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • SpringBoot-JWT生成Token和攔截器的使用(訪問受限資源)

    SpringBoot-JWT生成Token和攔截器的使用(訪問受限資源)

    本文主要介紹了SpringBoot-JWT生成Token和攔截器的使用(訪問受限資源),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • java?IP歸屬地功能實現(xiàn)詳解

    java?IP歸屬地功能實現(xiàn)詳解

    前一陣子抖音和微博開始陸續(xù)上了IP歸屬地的功能,引起了眾多熱議,有大批在國外的老鐵們開始"原形畢露",被定位到國內(nèi)來,那么IP歸屬到底是怎么實現(xiàn)的呢?那么網(wǎng)紅們的歸屬地到底對不對呢
    2022-07-07
  • Springboot在有參構(gòu)造方法類中使用@Value注解取值

    Springboot在有參構(gòu)造方法類中使用@Value注解取值

    這篇文章主要介紹了Springboot在有參構(gòu)造方法類中使用@Value注解取值,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Java編寫實現(xiàn)登陸窗口

    Java編寫實現(xiàn)登陸窗口

    這篇文章主要為大家詳細介紹了Java編寫實現(xiàn)登陸窗口,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java中TypeReference用法詳情說明

    Java中TypeReference用法詳情說明

    這篇文章主要介紹了Java中TypeReference用法詳情說明,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07

最新評論

当雄县| 正蓝旗| 登封市| 新沂市| 郑州市| 垫江县| 栾川县| 洪江市| 荥阳市| 鸡西市| 凉城县| 三江| 昌平区| 宁国市| 益阳市| 云安县| 荔波县| 荣成市| 固镇县| 德化县| 灯塔市| 新疆| 腾冲县| 南木林县| 横山县| 江川县| 常宁市| 克什克腾旗| 秭归县| 安平县| 四会市| 开原市| 宜章县| 延边| 太和县| 大荔县| 南阳市| 昭苏县| 新密市| 灵山县| 兴城市|