0
    Path src = Paths.get("./resources");
    Path dst = Paths.get("./trash");
    
    try {
        
        DirectoryStream<Path> ds = Files.newDirectoryStream(src);
            
        for(Path fileorDir : ds) {
            System.out.println(fileorDir);
            Files.copy(fileorDir, dst);
        }
    }catch(IOException ioe){
        ioe.printStackTrace();
    }

//The error im getting is java.nio.file.FileAlreadyExistsException so from what i understand its trying to save the file to that exact location, not inside it, i need to save a couple text files this way, if i change the destination address to say trash/trash.txt it will save a file there called trash.txt. but then on the next loop of the for each it throws a "Already exists" exception...

Can somebody explain how i can just save all txt files into that folder from the src folder, as if dragging and dropping them?

Many thanks

Fugazzie
  • 13
  • 2
  • 1
    You have an additional parameter of type `CopyOption` available to you; take a look at: https://docs.oracle.com/javase/7/docs/api/java/nio/file/StandardCopyOption.html – Richard Sitze Feb 19 '22 at 14:58

1 Answers1

0

You can use a option in copy() who is StandardCopyOption.REPLACE_EXISTING but the problem is that dst isn't the good path. For exemple, ressources/trash.txt should be copy in trash/trash.txt but dst is just /trash like path. Sorry for my english and it's my first answer :) Be merciful .

  • I did try with this previously but the issue with that is it will technically copy all files, but only the last file copied would be in the folder at the end as it replaces previously copied files each time. – Fugazzie Feb 19 '22 at 15:14
  • I'm not sure what you mean @user16320675, so ill say, i want 3 files for example, 1.txt, 2.txt, and 3.txt to all be copied from resources, to trash. The code there can fetch the file to be copied, but it cant copy it to the destination i need it to – Fugazzie Feb 19 '22 at 15:17
  • @user16320675 Yeah I clocked this is the issue, but I cant think of a way around it when moving X amount of files to one folder, hence me wondering if i cant just specify i want all the files putting into one folder – Fugazzie Feb 19 '22 at 15:27
  • I have looked, something worked, the resolve.getfilename worked but its saving it to the root folder now? whatever, its one step closer hahaha, i can jig this and make it work. thankyou for the help – Fugazzie Feb 19 '22 at 15:35
  • @user16320675 Files.copy(fileorDir, dst.resolve(fileorDir).getFileName()); is what i have that is putting it in the root folder, ive tried every other version in resolve parameters but it either doesnt run or does nothing – Fugazzie Feb 19 '22 at 16:04