簡體   English   中英

將所有文件從目錄復制到另一個目錄

[英]Copy all the files from a directory to another

我試圖將目錄中的所有文件復制到另一個目錄(但我希望它不復制文件夾)。 我正在嘗試使用Files.copy,但出現此錯誤:

Exception in thread "main" java.nio.file.FileAlreadyExistsException:

這是我的實際代碼:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class Exercici1 {

   public static void copiarArchivos(String pathSource,String pathOutcome, String sufix) throws IOException {
    File origen = new File(pathSource);
    String[] contenidoOrigen = origen.list();
    for(String string:contenidoOrigen){
        File interno = new File(origen,string);
        if (interno.isDirectory()){
            copiarArchivos(interno.getPath(),pathOutcome,sufix);
        } else {
            Path targetOutcome = Paths.get(pathOutcome);
            Path targetSource = Paths.get(interno.getPath());
            Files.copy(targetSource,targetOutcome);
        }
    }




}
public static void main(String[] args) throws IOException {

copiarArchivos("Vampiro_Mascarada","pruebaPDF",".pdf");
}
}

我的文件夾結構是這樣的:

/out
/pruebasPDF
/src
/Vampiro_Mascarada
    /1.pdf
    /2.pfdf
    /Images
        /1.png
        /2.png

您需要使用具有REPLACE_EXISTING選項的Files.copy(source,dest,CopyOption)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM