2017-09-21 52 views
0

我試圖將目錄內的所有文件複製到另一個目錄(但我希望它不復制文件夾)。我試圖用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 

回答

1

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

相關問題