• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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

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.
  • 2
1 Solution
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
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;
   public void run() {
     while (true) {
       if (time != file.lastModified()) {
         time != file.lastModified();
         reload(); // call the reload method
       Thread.sleep(2000); // 2 seconds
mwalkerAuthor Commented:
Thanks.  Although I was hoping that Java had something built-in to do this, your solution seems like it'll work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now