3

For those unfamiliar with JNotify, this is a library which provides an easy way to monitor events in a directory.

For instance, when a file gets deleted in the selected folder, the method "fileDeleted" gets called, along with a few parameters. Here's an example of the fileDeleted method:

public void fileDeleted(int wd, String rootPath, String name) {
   print("deleted " + rootPath + " : " + name);
}

Now, I would like to know if the deleted file was a file or directory. My usual approach is to create a new File object with the given path, and use the methods isFile() and isDirectory()

However, since this file is already deleted, these methods always return false.

So here's my concrete question: I have the path to a deleted file or directory, how can I tell wether it was a file or a directory? Is there a workaround to this? What's the best practice to do here?

Thank you in advance.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
Moeri
  • 9,104
  • 5
  • 43
  • 56

3 Answers3

3

I suggest using a better API for this, like Commons IO. It has this distinction in its interface org.apache.commons.io.monitor.FileAlterationListener and its methods onFile...(), onDirectory...(). Alternatively, and this is probably the best approach, use the new standard feature for this that comes with Java 7, WatchService, as discussed here.

Community
  • 1
  • 1
Robert Petermeier
  • 4,122
  • 4
  • 29
  • 37
  • Thanks, I've looked into it, but apparently the WatchService doesn't suppport RENAMED notifications. Although there is a third party library for it that adds this functionality, I discovered that I had a bit of the same issues with the WatchService as I had with JNotify (multiple notifications etc) In the end, I decided to use my server for knowledge about the deleted files, since there is no easy client solution. Thanks anyway! :) – Moeri Nov 27 '11 at 14:25
2

How big is the directory structure you're looking at?

My first instinct is to build an internal representation of the directory structure, using some simple graph traversal algorithm, and then do a lookup every time something is removed to figure out what it was.

<edit>
If you know your directory structure is a strict tree you can use a simple recursion to traverse the file system, and create a map of Files or Strings to boolean, so you can do an easy lookup. Then, once you've got the map built it should be easy to maintain using the JNotify events.
<edit/>

even for medium-sized directories I would think this could be made pretty quick. What is this for? Might there be another way of going about achieving the same goal?

Dogmatixed
  • 794
  • 1
  • 11
  • 33
  • depending on the sort of stuff in the directory you might only need a simple recursive algorithm (i.e., if there are no shortcuts that make the directory not a tree), and your files/directories could just be stored in a map, rather than building a graph, mapping location to a boolean isDirectory. Why don't you want to ask the server? If the server is tracking the files anyway, can't it just look at its own state? – Dogmatixed Nov 25 '11 at 19:12
  • Hi, just to give you an update: I've decided that the workaround to this problem from the client side would be a bit too elaborate, so I'm now looking at the server side if I'm dealing with a file or a directory, since the client didn't *really* need to know this. I won't mark your answer as accepted, since I don't want to confuse people who stumble upon this thread, and apparently I can't upvote you because I don't have enough reputation... so I'll just say thanks instead, for your time! :) – Moeri Nov 26 '11 at 16:14
  • No problem, I actually think Robert Petermeier's answer is probably better. Hope the project goes well! – Dogmatixed Nov 26 '11 at 17:04
0

I am facing the same problem. Yet as far as I understand it, Java's WatchService does not allow monitoring of subdirectories, so I cannot use it (task is to monitor changes to a structure containing ~40K folders). I will try and go ahead using the simple (and fallible) heuristic

If it contains a dot ('.'), it's a file.

I will post updates if I come across something more sophisticated...

f_puras
  • 2,521
  • 4
  • 33
  • 38