简体   繁体   中英

Copy all the files from a directory to another

i'm trying to copy all the files inside a directory to another(but i want it to not copy the folders). I'm trying to use Files.copy but i'm getting this error:

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

Here's my actual code:

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

My folder structure is like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM