Link to home
Start Free TrialLog in
Avatar of neokeet
neokeet

asked on

File Change

I want to check and see each time the file was modified.

I can get the last file modified status from :

file.lastModified(); method, but cannot think of the logic which will sayme if it changed or did not change from the last time.

For example:

if(file changed)
{
 //do something
}
else
{
// file did not change so do not do anything.
}

Thanks
Neo
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

The only thing You could check for is file.size(). There is no way You can check when and how it was changed.
If, in a separate thread, you cache the file's contents and monitor it periodically for changes, then you can keep track of when and how it was changed.  However, this will only work on one file at a time and only while the program is running.
Avatar of neokeet
neokeet

ASKER

I just need to monitor one file.
And this is where i am stuck right now.

I do not know if this is the right approach of somebody can give me a better solution.


import java.io.*;


public class polltest extends Thread {


      public void run() {


 
            String filename = "C:/test.csv";
            File file = new File(filename);
            long currentLastModified = 0;
            long tmp = 0;
            while (true) {

//                  Get the last modified time
                   long modifiedTime = file.lastModified();
                   // 0L is returned if the file does not exist
   
                   // Set the last modified time
                   long newModifiedTime = System.currentTimeMillis();
                   boolean success = file.setLastModified(newModifiedTime);
                   int i=0;
                   if (!success) {
                         i=i+1;
                        // operation failed.
                        System.out.println("File Changed "+i);
                   }
                   else
                   {
                         //
                   }

                  try {
                        this.sleep(5000);
                  }
                  catch (InterruptedException e) {
                        System.out.println("Interrupte­d!");
                  }
            }              
      }


      public static void main(String[] args) throws Exception {
            polltest tp = new polltest ();
            tp.setPriority(Thread.NORM_PRIORITY);
            tp.start();


      }


}
Rather, it will only work on the files you specify ahead of time.
Avatar of neokeet

ASKER

I did not get your point dberner9.

A little backgroud on what i am trying to do. The file "C:/test.csv" is constantly overwritten by an external program and i trying to find when it is changed.

That was in response to my previous comment, sorry.

First of all I would give your thread class a constructor that takes the file name as its parameter.  I would also return from the run() method if the file does not exist.  But those are more coding style than anything.

I'm not sure why you're trying to change the modified time of the file every time through the loop.  This would cause your thread to think the file has changed every time.

Here's what I would do for your thread loop:

File file = new File(fileName);
if (!file.exists()) return;
long lastModified = file.lastModified();
while(true) {
  long newLastModified = file.lastModified();
  boolean modified = (lastModified < newLastModified);
  lastModified = newLastModified;
  if (modified) {
    System.out.println("File changed: " + new Date(lastModified));
  }
  try { this.sleep(5000); }
  catch ( InterruptedException ex ) { throw new RuntimeException(ex); }
}
ASKER CERTIFIED SOLUTION
Avatar of dberner9
dberner9
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of neokeet

ASKER

Thanks