0
File content[] = new File("C:/FilesToGo/").listFiles();

for (int i = 0; i < content.length; i++){                       

    String destiny = "C:/Kingdoms/"+content[i].getName();           
    File desc = new File(destiny);      
    try {
        Files.copy(content[i].toPath(), desc.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
    }                   
}   

これは私が持っているものです。それはすべてをうまくコピーします。ただし、コンテンツの中にはいくつかのフォルダーがあります。フォルダはコピーされますが、フォルダの内容はコピーされません。

4

3 に答える 3

3

Apache Commons IO でFileUtilsを使用することをお勧めします。

FileUtils.copyDirectory(new File("C:/FilesToGo/"),
                        new File("C:/Kingdoms/"));

ディレクトリと内容をコピーします。

于 2012-08-11T22:12:38.270 に答える
0

再帰。以下は、再帰を使用してフォルダーのシステムを削除する方法です。

public void move(File file, File targetFile) {
    if(file.isDirectory() && file.listFiles() != null) {
        for(File file2 : file.listFiles()) {
            move(file2, new File(targetFile.getPath() + "\\" + file.getName());
        }
    }
    try {
         Files.copy(file, targetFile.getPath() + "\\" + file.getName(),  StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
    } 
}

コードをテストしませんでしたが、動作するはずです。基本的に、それはフォルダーを掘り下げて、アイテムを移動するように指示し、フォルダーの場合は、そのすべての子を調べて移動するなどします。

于 2012-08-11T22:08:31.893 に答える