0

I have checked other solutions on this websites and others but unable to find anything that remedies my problem. My path is definitely correct and the file definitely exists. Please also note that .getAbsoluteFile() makes no difference to the results I'm getting.

I am encountering some strange behaviour in my standalone Spring application. Inside a WatchEvent I detect any new files being created and I try to create a File object from it. The odd thing is I can print the name of the file but isFile() returns false and length() returns 0. I have tested with lots of different files but I get the same results. The file(s) are local.

Here is the relevant code snippet:

public void monitor() {
        try {
            while (key.isValid()) {
                for (WatchEvent<?> event : key.pollEvents()) {
                    if (event.kind() == ENTRY_CREATE) {
                        System.out.println("New file detected in FTP Folder");
                        WatchEvent<Path> ev = cast(event);
                        System.out.println("EV Context: " + ev.context());
                        System.out.println("EV Context TO STRING: " + ev.context().toString());
                        System.out.println("EV Context Absolute File: " + new File(ev.context().toString()).getAbsoluteFile());
                        File file = new File(ev.context().toString()).getAbsoluteFile;                            System.out.println("File Name is " + file.getName());
                        System.out.println("Is file " + file.isFile());
                        System.out.println("Is Directory " + file.isDirectory());
                }
            }
            key.reset();
        }
    } catch (NullPointerException e) {
        System.out.println(e.getMessage());
    }
}

and the output is as follows:

New file detected in FTP Folder
EV Context: <the file name in printed correctly with extension>
EV Context Absolute File: <prints correct full path with extension>
File Name is <prints correct file name>
Is file false
is Directory false

The value returned by isFile() is false even though it's able to print the correct file name, the file definitely exists, the file is in a directory that is in the project folder.

When a folder is placed into the Watched Folder then I still get false for directory.

I can't figure out what I'm doing wrong here...

Thanks.

  • Have you checked this thread: https://stackoverflow.com/questions/14853402/both-file-isfile-and-file-isdirectory-is-returning-false ? Is it possible to read file? – y.bedrov Jan 11 '18 at 14:24
  • Hi, I have seen that thread. I have full permissions to modify the file. file.canRead() returns false :/ –  Jan 11 '18 at 14:30
  • As `ev.context()` is a Path, did you try `Files.isRegularFile(ev.context()), isReadable, isDirectory, toAbsolutePath`? – Joop Eggen Jan 11 '18 at 15:05
  • Hi, thanks for your suggestions. I get a NoSuchFileException for any methods I use when using Files object. I have checked the absolute path and it's 100% correct. –  Jan 11 '18 at 15:29
  • I am an idiot. The string returned by test.toAbsolutePath() is incorrect so that'll be a clue as to why it's not working. Thank you, I might be able to fix it from here. –  Jan 11 '18 at 15:31

0 Answers0