Kopieren von Dateien aus einem Ordner in einen anderen

DasDarki

Mitglied
Also, ich bin grade dabei zu versuchen alle Dateien in einem Ordner in einen anderen Ordner zu kopieren. Soweit gibt es keine Fehler, doch der Ordner wo die Dateien reinkopiert hätten werden sollen, ist immer leer nach dem Kopieren.
Hier die Methoden zum Kopieren:
Java:
public static void copyTemplates(String pathSource, String pathTarget){
        try{
            File file = new File(pathSource);
            File[] files = file.listFiles();
            for(File in : files){
                System.out.println(Daemon.prefix + "Copying File: " + in.getName() + " from " + in.getPath() + " to " + pathTarget);
                copyFile(in, pathTarget);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
   
    private static void copyFile(File source, String pathTarget) throws IOException{
        (new File(pathTarget)).mkdirs();
        File target = new File(pathTarget);
        if(!target.exists())target.createNewFile();
        Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

Und hier die Anwenungsklasse:
Java:
public void start(){
        try{
            ProcessBuilder pb = new ProcessBuilder("java",
                    "-Xms"+ minRam +"M",
                    "-Xmx"+ maxRam +"M",
                    "-XX:MaxGCPauseMillis=50",
                    "-XX:ParallelGCThreads=2",
                    "-XX:+UseParallelGC",
                    "-jar",
                    getPath() + "NiloCloud/jars/spigot.jar");
            pb.redirectErrorStream(true);
            pb.directory(new File(getPath() + "NiloCloud/Servers/" + uuid));
            FileUtils.copyTemplates(Daemon.templateManager.getDir(gm).replace("\\", "/"), (getPath() + "NiloCloud/Servers/" + uuid));
            Daemon.registerManager.register(RegisterStatus.NEW, gm, uuid, GameStatus.LOBBY, port);
            new PortEditor(getPath() + "NiloCloud/Servers/" + uuid + "/server.properties", port.toString()).change();
            Process process = pb.start();
            this.process = process;
            System.out.println("Process started!");
        }catch(Exception ex){}
    }

    private String getPath() throws URISyntaxException{
        String s = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        s = s.replace("ncd.jar", "");
        return s.substring(1);
    }
 
In der API steht

REPLACE_EXISTING – Performs the copy even when the target file already exists. If the target is a symbolic link, the link itself is copied (and not the target of the link). If the target is a non-empty directory, the copy fails with the FileAlreadyExistsException exception.

Ansonsten würde, ich sagen, sieht doch gut aus.
Könnte entweder an dem oben gelegenen Zitat was dran sein oder aber dein Destination-Target ist nicht ganz stimmig?

getPath() + "NiloCloud/Servers/" + uuid

Hast du dir das mal angeguckt was dabei raus kommt?

Edit: Was mir auffällt. Du versuchst alle Dateien auf den gleichen Pfad zu kopieren, aber so funktioniert das nicht ganz, denn du vergisst, dass du einen Dateinamen mitgibst. Versuch mal
Code:
Files.copy(source.toPath(), target.toPath()+source.getFilename, StandardCopyOption.REPLACE_EXISTING);
 
Zuletzt bearbeitet:
Für meine Begriffe fehlen da an einigen Stellen die File.SEPARATOR d.h. "/" .

Edit: oder ist es Absicht dass du hier nie einen / dazwischen machst:
getPath() + "NiloCloud/Servers/"
 
Vermutlich gibt es Fehler, du ignorierst sie nur

Ein leerer catch block ist so ziemlich das schlimmste was man machen kann. Denn wenn nun was schief, merkst du es nichtmals
Also Fehler kommen keinen, ich habe den Try Catch Block mal weggenommen, immer noch keine Fehler.
Ich habe mir mal ausgeben lassen von wo, wohin und was kopiert werden soll:
Code:
Copying File: banned-players.json from D:\NiloCloudTest\Daemon\NiloCloud\templates\Beispiel\banned-players.json to D:/NiloCloudTest/Daemon/NiloCloud/Servers/3c954897-2142-4a74-9a40-2d37ac289869
 
Edit: Was mir auffällt. Du versuchst alle Dateien auf den gleichen Pfad zu kopieren, aber so funktioniert das nicht ganz, denn du vergisst, dass du einen Dateinamen mitgibst. Versuch mal
Code:
Files.copy(source.toPath(), target.toPath()+source.getFilename, StandardCopyOption.REPLACE_EXISTING);

Verbesserung: du gibst den Dateinamen nicht mit. Soweit ich weiß, muss Source und Target auch die Datei beinhalten, die es zu kopieren gilt. Einfach die Elternpfade in Files.copy zu stecken reicht nicht.
An dein targetPath müsste also noch der jeweilige Dateinamen. Oder hast du das schon probiert?

Bspw so:
Code:
Path FROM = Paths.get("C:\\Temp\\from.txt");
    Path TO = Paths.get("C:\\Temp\\to.txt");
    //overwrite existing file, if exists
    CopyOption[] options = new CopyOption[]{
      StandardCopyOption.REPLACE_EXISTING,
      StandardCopyOption.COPY_ATTRIBUTES
    };
    Files.copy(FROM, TO, options);
 

Zurück
Oben