This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes File Change Notification Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "File Change Notification" Watch "File Change Notification" New topic
Author

File Change Notification

Tongyu Wang
Greenhorn

Joined: Sep 10, 2002
Posts: 8
Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.


SCJP, SCJD<br />"He who knows others is wise; He who knows himself is enlightened."<br />-- Lao-Tzu (6th century B.C.), Legendary Chinese philosopher
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Tongyu Wang:
Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.


I think you answered your own question there, you're looking for something that "monitors" for changes and notifies some class or does something about it itself.
This implies a thread, maybe using a Timer, and it doesn't necessarily have to be expensive.
I may be wrong, haven't slept well enough
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead

Which overhead?
You want to do it, but you don't want to do it?
Do you want someone else to do it?
Without overhead?
Which overhead?

You may write a filesystem, with integrated file-change-monitors.
Or you could save to a rdbms, and create a trigger, which fires on every change...

That doesn't sound like overhead - does it
[ July 12, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Tongyu Wang
Greenhorn

Joined: Sep 10, 2002
Posts: 8
No. I meant some kind of notification object in the Java API I can "wait" on and receive notification when change happens. I know win32 has a FindFirstChangeNotification call to get such a handle but not sure if Java API exposes it.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I've never seen an Java version of this. It does seem like something which the standard libraries should provide, but as far as I know, they don't. I hate making a wait-and-recheck busy loop - seems like there should be an addFileChangeListener() method somewhere. If anyone else knows of one, I'd like to hear of it.


"I'm not back." - Bill Harding, Twister
amit lkumar
Greenhorn

Joined: Mar 06, 2009
Posts: 4
Tongyu Wang wrote:Does anyone know how to monitor a file for changes (content, attributes, etc)? Sure I can spin a thread to check for last modified date once in a while, but I don't want the overhead. Thanks in advance.


You can have a look here : http://java.sun.com/docs/books/tutorial/essential/io/notification.html
I know the post is very old, but it may help many..


Amit
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12956
    
    3

Amit, welcome to JavaRanch.

Usually, we don't like it if someone wakes the zombies, but I guess your answer will be useful if anyone is searching for this.

But do note that the watch service API is not yet available even in today's version of Java (Java 6); this is a new feature that's going to be available in Java 7, which will not be released before September 2010. So, unfortunatelty most people today will not yet be able to use this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: File Change Notification
 
Similar Threads
File constructor
requested resource (first.jsp) not available
Ant error: Make sure you have it in you classpath
Adding a file to a program
JMF