Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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