1

Asked this question, having already tried possible solutions in other questions here on stack but that didn't allow me to fix the problem.

As in the title, I have created a java utility with which I have to perform operations on text files, in particular I have to perform simple operations to move between directories, copy from one directory to another, etc.

To do this I have used the java libraries java.io.File and java.nio.*, And I have implemented two functions for now,copyFile(sourcePath, targetPath) and moveFile(sourcePath, targetPath). To develop this I am using a mac, and the files are under the source path /Users/myname/Documents/myfolder/F24/archive/, and my target path is /Users/myname/Documents/myfolder/F24/target/.

But when I run my code I get a java.nio.file.NoSuchFileException: /Users/myname/Documents/myfolder/F24/archive

Having tried the other solutions here on stack and java documentation already I haven't been able to fix this yet ... I accept any advice or suggestion

Thank you all

  • my code:
// copyFile: funzione chiamata per copiare file
    public static boolean copyFile(String sourcePath, String targetPath){

        boolean fileCopied = true;
        
        try{

            Files.copy(Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);

        }catch(Exception e){
            String sp = Paths.get(sourcePath)+"/";
            fileCopied = false;
            System.out.println("Non posso copiare i file dalla cartella "+sp+" nella cartella "+Paths.get(targetPath)+" ! \n");
            e.printStackTrace();
        }


        return fileCopied;
    }
9
  • try chmod -R 777 /Users/myname/Documents/myfolder/F24/archive/ first? Commented Dec 1, 2021 at 8:52
  • Are you trying to move or copy directories including their contents? If yes, then check this question Commented Dec 1, 2021 at 8:55
  • 1
    Sometimes overseen: targetPath must not be a directory but the full path of the (possibly yet not existing) target file - with file name. Also the copy options are tender. Commented Dec 1, 2021 at 8:56
  • Hi, as the first operation I gave 777 permissions to the folder but it keeps giving me exception ... the thing that puzzles me is that the path where I have the files is /Users/myname/Documents/myfolder/F24/archive/ but the exception shows me as path` /Users/myname/Documents/myfolder/F24/archive` i.e the last / is missing ... I don't know if that could be Commented Dec 1, 2021 at 8:57
  • @deHaar No, i want copy only the file not the directory Commented Dec 1, 2021 at 8:58

2 Answers 2

2

Files.copy cannot copy entire directories. The first 'path' you pass to Files.copy must ALL:

  • Exist.
  • Be readable by the process that runs the JVM. This is non-trivial on a mac, which denies pretty much all disk rights to all apps by default until you give it access. This can be tricky for java apps. I'm not quite sure how you fix it (I did something on my mac to get rid of that, but I can't remember what - possibly out of the box java apps just get to read whatever they want and it's only actual mac apps that get pseudo-sandboxed. Point is, there's a chance it's mac's app access control denying it even if the unix file rights on this thing indicate you ought to be able to read it).
  • Be a plain old file and not a directory or whatnot.

Files.move can (usually - depends on impl and underlying OS) usually be done to directories, but not Files.copy. You're in a programming language, not a shell. If you want to copy entire directories, write code that does this.

Sign up to request clarification or add additional context in comments.

Comments

2

Not sure whether my comment is understood though answered.

Ìn java SE target must not be the target directory. In other APIs of file copying one can say COPY FILE TO DIRECTORY. In java not so; this was intentionally designed to remove one error cause.

That style would be:

        Path source = Paths.get(sourcePath);
        if (Files.isRegularFile(source)) {
            Path target = Paths.get(targetPath);
            Files.createDirectories(target);
            if (Files.isDirectory(target)) {
                target = Paths.get(targetPath, source.getFileName().toString()); 
                // Or: target = target.resolve(source.getFileName().toString());
            }
            Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        }

Better ensure when calling to use the full path.

2 Comments

Hi Joop, i've edited my copyFile like your code, but the problem still exists, I get the following error: I can't copy file from folder/Users/myname/Documents/myfolder/F24/archive into folder/Users/myname/Documents/myfolder/F24/target/archive
I have added code. But the error seems to indicate that you copy the file to itself. That is not possible, as when open a file for reading, you no longer can write to it (for copying with replace that is). Also others already mentioned directories are copied using Files.walk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.