java,filesystems,in-memory,nio2
You cannot really load this directly into memory. You do have FileChannel.open() from which you can then .map() but that will create a temporary entry on your disk anyway. There is also memoryfilesystem, but it will not handle files big enough for it to be useful, unfortunately. The best solution...
I think it is because you auto close the Channel when you leave the try {...} block and the write you are doing is async, which means that if your write is not done fast-enough you will try to write to a closed channel.
In my honest opinion this is the simplest way to assure, that you're still handling events for this WatchKey/Path mapping. In case there are two events for the same file in the queue. While handling event #1 you decide to stop handling events for this Path. After that you retrieve...
Your last comment seems to describe exactly what happens here. I guess the DirectoryStream adds (or adds not) newly saved files without checking for their prior listing. In this case, it's probably a good idea to add the entries from the DirectoryStream to another Iterable or even Set (e.g. HashSet)...
Using java.nio available since Java 7: Path file = Paths.get("/Music/Beatles/Help.mp3"); Path dir1 = Paths.get("/"); Path dir2 = Paths.get("/Music"); Path dir3 = Paths.get("/Music/Beatles"); System.out.println(dir1.relativize(file)); System.out.println(dir2.relativize(file)); System.out.println(dir3.relativize(file)); You get: Music/Beatles/Help.mp3 Beatles/Help.mp3 Help.mp3 ...