Solved

Java Code needed to be notified when a file has been modified

Posted on 2002-05-23
3
193 Views
Last Modified: 2010-03-31
I was wandering if there's a way for a running piece of code that has a reference to a File to be notified when the text file, for example, was modified.  The specific situation I have is that if I have a text file that has a number of properties for the class, then the class will be notified to reload the properties without having to be restarted.

This is intended to run with an application server that I don't believe I can use JDK 1.4.  I'd be using JDK 1.3.x.

Thanks in advance.
0
Comment
Question by:mwalker
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7030024
You can do it by running a thread that checks every (for example) 2 seconds the file lastModified() value. When it changes - the thread can the reload() method
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 150 total points
ID: 7030033
The thread should be something like this:


class Notifier extends Thread {
   private long time;
   private File file;
   public Notifier(File f) {
      time = f.lastModified();
      file = f;
      start();
   }
   
   public void run() {
     while (true) {
       if (time != file.lastModified()) {
         time != file.lastModified();
         reload(); // call the reload method
       }
       Thread.sleep(2000); // 2 seconds
     }
   }
}
0
 

Author Comment

by:mwalker
ID: 7030198
Thanks.  Although I was hoping that Java had something built-in to do this, your solution seems like it'll work.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question