在Java中,如何使用Path对象复制目录中的所有文件?

时间:2018-01-20 00:50:34

标签: java path

请善待,我是Java的新手。我正在寻找一种方法,使用Path对象将所有文件从源目录复制到目标目录。我环顾四周,看到了这个问题的旧解决方案(使用较旧的java,io),但没有(较新的java.nio)Path对象。这是我到目前为止所拥有的。

// copyFiles - Copies files from source directory/folder to destination directory/folder
// Requires:
//      import java.nio.file.Path;
//      import java.nio.file.Paths;
//      import java.nio.file.Files;
public static void copyFiles(String OSType, String source, String destination)throws IOException {
    Path sourcePath = Paths.get(source);
    Path destinationPath = Paths.get(destination);

    if (!Files.exists(sourcePath) || !Files.isDirectory(sourcePath)) {
        System.out.println("Source directory/Folder " + source + " does not exists.");
    } else if (!Files.exists(destinationPath) || !Files.isDirectory(destinationPath)) {
        System.out.println("Destination directory/Folder " + source + " does not exists.");
    } else if (Files.list(sourcePath).count() > 0) {

        List<Path> sourceLocationContent = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourcePath)) {
            for (Path file : stream) {
            // System.out.println(file.getFileName());
            sourceLocationContent.add( file.getFileName());
        }

        System.out.println("sourceLocationContent.size() = " + sourceLocationContent.size());
        for (int i=0 ; sourceLocationContent.size() > i ; i++){
            System.out.println("sourceLocationContent.get("+i+") = " + sourceLocationContent.get(i));
            Files.copy(sourceLocationContent.get(i),destinationPath);
        }
    } catch (IOException | DirectoryIteratorException exception) {
        System.out.println(exception);
    }
} else {
    System.out.println("Source directory/Folder " + source + " is empty.");
}

}

1 个答案:

答案 0 :(得分:0)

这是我执行此操作的稍长代码。 这个例子可以立即运行 确保提供src和dest目录。

    package copytodir;

import static java.nio.file.StandardCopyOption.*;
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.*;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Chol
 */
public class CopyToDir {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter source: ");
        String srcDir = keyboard.nextLine();
        System.out.print("Enter destination: ");
        String destDir = keyboard.nextLine();
        copyDirectory(srcDir, destDir);
    }

    public static void copyDirectory(String sourceDir, String destDir) throws IOException {
        Path sourceDirPath = Paths.get(sourceDir);
        Path destDirPath = Paths.get(destDir);

        Files.copy(sourceDirPath, destDirPath, REPLACE_EXISTING);
        Files.list(sourceDirPath)
        .forEach(pathOfFile -> {
            try {
                copyFile(pathOfFile, Paths.get(destDirPath.toFile().getAbsolutePath() + File.separator + pathOfFile.toFile().getName()));
            } catch (IOException ex) {
                Logger.getLogger(CopyToDir.class.getName()).log(Level.SEVERE, null, ex);
            }
        });

    }

    public static void copyFile(Path srcFile, Path destFile) throws IOException {
        Files.copy(srcFile, destFile, REPLACE_EXISTING);
    }
}