我們在 Java 中有以下任務:
給定一個目錄和檔案串列,然后是目標,將所有這些目錄(遞回)和檔案復制到目標中。注意:給定目錄中的所有子目錄和檔案必須保存在目標檔案夾中的相同嵌套結構中。
我們將檔案陣列和目標檔案夾作為輸入。輸出必須產生如下內容:
Started: resources\docs\books... Started: resources\docs\books\american... Started: resources\docs\books\american\London... Started: resources\docs\books\american\London\BEFORE ADAM.txt... Finished FILE: resources\docs\books\american\London\BEFORE ADAM.txt Total 229KB were copied! Started: resources\docs\books\american\London\THE IRON HEEL.txt... Finished FILE: resources\docs\books\american\London\THE IRON HEEL.txt Total 528KB were copied! Started: resources\docs\books\american\London\The People of the Abyss.txt... Finished FILE: resources\docs\books\american\London\The People of the Abyss.txt Total 370KB were copied! Finished FOLDER: resources\docs\books\american\London Finished FOLDER: resources\docs\books\american
有沒有人有任何想法?
uj5u.com熱心網友回復:
您可以通過實作 FileVisitor 介面輕松實作此目的。這是一個代碼示例,只是為了向您展示它是如何作業的。它應該按照你的要求做。
編輯:更新了自定義尺寸處理
public class FileCopier implements FileVisitor<Path> {
private final Path sourcePath;
private final Path targetPath;
private HashMap<String, Long> mapSize;
public FileCopier(Path sourcePath, Path targetPath) throws IOException {
//Check whether the destination directory exists or not
if (!Files.exists(targetPath)) {
Files.createDirectories(targetPath);
}
this.sourcePath = sourcePath.toRealPath();
this.targetPath = targetPath.toRealPath();
this.mapSize = new HashMap<>();
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
//Copying the directory first and then its content (directory copy does not copy its content)
//Retrieving the last bit of the path starting from the source location
Path lastBitPath = sourcePath.relativize(dir.toAbsolutePath().normalize());
//Adding the last bit to the target path
Path targetCopyPath = targetPath.resolve(lastBitPath).toAbsolutePath().normalize();
if (!Files.exists(targetCopyPath)) {
Files.copy(dir, targetCopyPath, StandardCopyOption.COPY_ATTRIBUTES);
}
//Defining 0 bytes for the new visited directory
mapSize.put(targetCopyPath.toString(), 0L);
System.out.println("Started: " targetCopyPath);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path targetCopyPath = targetPath.resolve(sourcePath.relativize(file.toAbsolutePath().normalize())).toAbsolutePath().normalize();
Files.copy(file, targetCopyPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
//Adding the current copied file's bytes to the corresponding folder
mapSize.put(targetCopyPath.getParent().toString(), mapSize.get(targetCopyPath.getParent().toString()) Files.size(file));
System.out.println("Finished FILE: " targetCopyPath);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//Retrieving the last bit of the path starting from the source location
Path lastBitPath = sourcePath.relativize(dir.toAbsolutePath().normalize());
//Adding the last bit to the target path
Path targetCopyPath = targetPath.resolve(lastBitPath).toAbsolutePath().normalize();
//Showing the number of bytes copied within the copied directory
System.out.println("Total " this.mapSize.get(targetCopyPath.toString()) " bytes were copied into " targetCopyPath);
return FileVisitResult.CONTINUE;
}
public static void main(String[] args) throws IOException {
Path sourcePath = Paths.get("./test");
Path targetPath = Paths.get("./subtest");
FileCopier fc = new FileCopier(sourcePath, targetPath);
Files.walkFileTree(sourcePath, fc);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460989.html
上一篇:遞回回傳一個串列
