Solved

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

Posted on 2002-05-23
3
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

690 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