aspose file tools*
The moose likes Features new in Java 7 and the fly likes How to Watch directory in Swing GUI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "How to Watch directory in Swing GUI?" Watch "How to Watch directory in Swing GUI?" New topic
Author

How to Watch directory in Swing GUI?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 388
I found this nice console mode example of watching a directory and it even works!
Watch Directory example

This code looks a little CPU intensive: we are in a nested for loop! Are we actually polling the file system continually? Yikes! In C++ on windows, you set up a call back so you can sleep instead of poll.

Can anyone show me a Swing GUI example that demonstrates this directory watch feature?

Now I did a little google searching with no luck. How would I write a Swing GUI version of this example? Do I have to execute the watchFile function in a child thread and then use the javax.swing.SwingWorker from java1.6? How would I join with such a thread when I am exiting the application?

Thanks!
Siegfried

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

http://download.oracle.com/javase/tutorial/essential/io/notification.html


luck, db
There are no new questions, but there may be new answers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

Siegfried Heintze wrote:This code looks a little CPU intensive: we are in a nested for loop! Are we actually polling the file system continually? Yikes! In C++ on windows, you set up a call back so you can sleep instead of poll.

No, it is not polling the file system and it is not CPU intensive. The watcher.take() call in line 21 of the code example is a blocking call: it makes the thread wait (on the OS level) until some interesting event happens.

The whole idea of the file system watcher API is that on operating systems that support it, it will use the OS'es native support. So under the covers it's using Windows' native support for watching changes in a directory.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to Watch directory in Swing GUI?