File system events with Java 7

In the last post, I showed how to listen to Linux file system events using C, Ruby and Python.
In this post, we look at Java 7. Java 7 has several new classes in the java.nio.file package that let you listen to file system events. wholesale NBA jerseys The number of events available is not as extensive as the ones in the C, Ruby and Python example.
If you want to use the inotify mechanism directly in Java, look at the following libraries:
JNotify
inotify-java

In this example, we only look at the features of the new classes that come with Java 7. For this test, I used jdk-7-ea-bin-b144 64 bit on a Linux machine. It should work exactly like that when Java 7 is final.

Here is the source code:

This is a simple example on how to use the new classes. I created a new Thread that listens in an infinite loop for changes in the directory “/tmp/java7”. For each event (when a file is created, modified or deleted), the event and the file name is printed to Stdout. Note that this also works when creating or deleting directories.

Basically you create a WatchService, register the directory to watch (with the events to watch for), loop forever, create a WatchKey and poll on the WatchKey for events, then go over the events and do something with them, like printing as in this example. When done processing the events, reset the WatchKey so that it can contain new events.
The method reset returns true if the WatchKey is still valid. When you delete the watched directory, it returns false and in this example, the code breaks out of the while loop and terminates.

Note that in a real production system, you would probably not use System.out.println but do something else, like updating the directory view in a file manager, sending an email (for example, when watching a directory for activities that are not allowed, etc) or other actions.

In this example, I interrupt the program after 10 seconds. This is just to show you how to end watching a directory.

To test it, create the directory “/tmp/java7” and then create, modify and delete a few files in it. To see the reset method in action, remove the directory. If you want to play longer than 10 seconds, just remove the call to interrupt at the end of the main method.

For more information, see the javadoc of Java 7:
http://download.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html

“Until they do, then I don’t expect that we will change our view.”The NFL Players Association said, “Marijuana is currently a banned substance under the collectively bargained Substances of Abuse Policy. Both parties to the Policy (NFL and NFLPA) seek guidance from the independent medical professionals who administer the policy, and no change to marijuana’s status as a banned substance has been recommended by those medical professionals.”But Monroe said it is time for more research to look at the possible benefits of medical marijuana, especially as he sees players turning to it. Amid all the high rollers and glitz of the Las Vegas Strip are a surprising number of things to do that won’t cost you a thing. The casinos alone cheap jordan are a sensory tour de force, with lights, fountains, shows, and music much of it free. Ethel M Chocolate Factory is located in Henderson, Nevada, about 15 minutes by car from the Las Vegas Strip.During the treatment, the ‘RICE’ therapy is strictly followed. RICE stands for rest, ice, compression and elevation. Anti inflammatory medicines are often prescribed to alleviate swelling and to reduce pain. In severe and chronic cases, splints, taping and other supportive orthopedic devices are suggested. For grade 3 foot sprains, surgery has to be undergone to repair the damage.Neoprene compression wraps and braces are elastic enough to conform to the contours of your body. They come http://www.cheapoakley2012.com in varieties designed for nearly every body part as well as universal wraps for general application. Velcro fasteners help ensure a snug fit so that these neoprene wraps can offer solid support for hours at a time.Newton oakley sunglasses runs, throws for TDs; Panthers dump Redskins Cam Newton (1) of the Carolina Panthers rushes for a second half touchdown during their game against the Washington Redskins at Bank of America Stadium on Oct. The No. 1 overall draft pick ran for a touchdown and threw for another, finishing with 256 yards passing and running for 59 more as Cheap ray bans the Panthers beat the Redskins 33 20.He is too decent, too loyal, to draw the timely parallel with Arsenal’s broader problems. He is an unwitting symbol wholesale football jerseys of what the club has sacrificed since its move from the spiritual home, Highbury. The business plan still promotes the mythology of “The Arsenal Way” but it is the institution itself which has lost its way.Resting is the best thing one can do to help promote healing, because movement aggravates the problem. It is best to avoid oakley sunglasses outlet all possible activities Cheap china Jerseys that can cause strain to the bruised area. Activities like bending, lifting, carrying, etc. should be avoided for a few days. Lying down on the bed, either sleeping, watching television or reading a book, can prevent unwanted strain.

1 Comment

  1. Varun says: Reply

    Above code works but there is a problem:
    Issue: When I create a file inside the folder and try to delete the folder. then this program keep waiting and will not monitor any more events.
    It works well only if folder is empty before deleting.
    Solutions:
    import java.nio.file.Files;

    if (Files.notExists(path)){
    System.out.println(“No longer valid”);
    watchKey.cancel();
    watchService.close();
    break;
    }
    ———————————————–
    Removed code that starts with if (!watchKey.reset()) {

Leave a Reply

This blog is kept spam free by WP-SpamFree.