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;
    }
64Bit1990
  • 302
  • 2
  • 16
  • try `chmod -R 777 /Users/myname/Documents/myfolder/F24/archive/` first? – 袁文涛 Dec 01 '21 at 08:52
  • Are you trying to move or copy directories including their contents? If yes, then check [this question](https://stackoverflow.com/questions/15137849/java-using-nio-files-copy-to-move-directory) – deHaar Dec 01 '21 at 08: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. – Joop Eggen Dec 01 '21 at 08: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 – 64Bit1990 Dec 01 '21 at 08:57
  • @deHaar No, i want copy only the file not the directory – 64Bit1990 Dec 01 '21 at 08:58
  • @JoopEggen yes, my target path is the full path and is empty – 64Bit1990 Dec 01 '21 at 09:00
  • @JoopEggen - I'm not sure what you mean by "tender" ... – Stephen C Dec 01 '21 at 09:19
  • @StephenC still did not drink my coffee; I meant copy options can be picked wrong, the defaults often are sufficient. But that was a very general warning for beginners - if something goes wrong: check the options. – Joop Eggen Dec 01 '21 at 09:40
  • Maybe "sensitive" would have been a better word. "Tender" means either loving or painful or ... the railway truck behind a steam locomotive that holds the coal :-) – Stephen C Dec 01 '21 at 10:01

2 Answers2

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 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` – 64Bit1990 Dec 01 '21 at 10:22
  • 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` – Joop Eggen Dec 01 '21 at 11:22