I'm writing a program that watches a certain directory for any new files, and whenever new files arrive, the program takes some action on those files. I'm using WatchService to watch the directory, so I feel like it can be conclusively proven that the file does indeed exist.
However, whenever I attempt to create a FileReader on the file that was found by my WatchService, I get a FileNotFoundException.
I am making sure that Java is using the Absolute Path when attempting to create the FileReader, so I have no reason to believe its looking in the wrong place.
I've done lots of research on FileReader, FileNotFoundException, and both the File and Path objects, yet I'm still unable to determine why this exception is being thrown.
I'm not too familiar with WatchService, but I used code that I have found on other forums and it seems to be detecting the file just fine and passing it through. Code is below.
try
{
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get("C:\\sample\\path")
WatchKey key = dir.register(watcher, ENTRY_CREATE);
for(;;)
{
try
{
key = watcher.take();
}
catch(InterruptedException exception)
{
//code
}
finally
{
for(WatchEvent<?> event: key.pollEvents())
{
WatchEvent.Kind<?> kind = event.kind();
if (kind == OVERFLOW)
{
continue;
}
WatchEvent<Path> trigger = (WatchEvent<Path>)event;
Path filename = trigger.context();
filename = filename.toAbsolutePath();
File theFile = filename.toFile();
//EXCEPTION IS THROWN HERE
FileReader fReader = new FileReader(theFile);
/**
* more code
**/
}
}
}
}
catch(IOException exception)
{
//code
}
As stated in the code block, a java.io.FileNotFoundException is thrown after I try to create a FileReader based on the file. I know that the file exists because my WatchService detected the file being created and provided the file's path with trigger.context().
I even made sure that FileReader is using the absolute path of the file by calling toAbsolutePath() on the file.
I have a debug statement right before the problematic line of code that prints the path of the file in question, and yes, the path that is printed is the correct absolute path of the file. So why does FileReader not detect the file?
I hope somebody can help me, I realize this post is exceptionally long, but I am not sure if the problem is being caused by my WatchService, or the File, or the FileReader, so I wanted to include as much information as possible.
Thank you very much.
I know that the file exists because my WatchService detected the file being created and provided the file's path with trigger.context().
No it didn't.
You failed to query for the type of the event. The JDK provides these standard types, yet you only check whether there's an overflow.
You have to check whether the event received is actually either a modification or a creation (ie, StandardWatchEventKind.EVENT_{CREATE,MODIFY}
).
What is more, don't use File
. Use Path
all the way.
Once you have ensured that the event is a creation or modification, then open an InputStream
or a Reader
to the file using either Files.newInputStream()
or Files.newBufferedReader()
. If, using this, you still get an exception, at least it will be a much more meaningful one than FileNotFoundException
(read the link I posted above...).