Solved Copy files to folder

Discussion in 'Spigot Plugin Development' started by StereoN, Aug 10, 2018.

  1. Hello there, I'm trying to make a backup for my plugin's files, but the method doesn't seem to copy the files into the directory.

    Code (Text):

    public void backupFiles()
        {
            configf = new File(getDataFolder(), "config.yml");          
            dataf = new File(getDataFolder(), "data.yml");
            warpf = new File(getDataFolder(), "warps.yml");
            spawnf = new File(getDataFolder(), "spawn.yml");
            jailf = new File(getDataFolder(), "jail.yml");
            kitsf = new File(getDataFolder(), "kits.yml");
            chatf = new File(getDataFolder(), "chat.txt");
            commandsf = new File(getDataFolder(), "commands.txt");
           
            ArrayList<File> files = new ArrayList<File>();
            String path = "/backups/";
           
            files.add(configf);
            files.add(dataf);
            files.add(warpf);
            files.add(spawnf);
            files.add(jailf);  
            files.add(kitsf);
            files.add(chatf);
            files.add(commandsf);
           
            for(File file : files)
            {
               
                try
                {
                    Path dst = new File(path).toPath();
                    Path src = file.toPath();

                    java.nio.file.Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);      
                   
                    Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&c&lBackup: &fBackup was succesful!"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
               
               
            }
     
    From what I've seen, Files.copy should also move the files to the directory, any ideas why it isn't working?
     
  2. Is this path sufficient?
    Try the full path or get the path from the file and add "/backups/".
    You might save the files into some JVM root directory now...
     
    • Informative Informative x 1
  3. By the full path you mean /plugin/MyPlugin/backups or /root/server..etc?
     
  4. Problem solved using Apache Commons FileUtils
     
  5. No, the full path didn't work either, i think File.copy reffers to 2 distinct files, and i wanted to copy the files in an empty directory, that's why it didn't work.