?
Solved

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

Posted on 2002-05-23
3
Medium Priority
?
217 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 600 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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