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.