2017-09-21 1 views
0

J'essaie de copier tous les fichiers d'un répertoire à un autre (mais je ne veux pas copier les dossiers). Je suis en train d'utiliser Files.copy mais je reçois cette erreur:Copier tous les fichiers d'un répertoire à un autre

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

Voici mon code actuel:

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"); 
} 
} 

Ma structure de dossier est comme ceci:

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

Répondre

1

Vous devez utiliser Files.copy (source, dest, CopyOption) avec l'option REPLACE_EXISTING.