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
neokeetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
The only thing You could check for is file.size(). There is no way You can check when and how it was changed.
dberner9Commented:
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.
neokeetAuthor Commented:
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();


      }


}
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

dberner9Commented:
Rather, it will only work on the files you specify ahead of time.
neokeetAuthor Commented:
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.

dberner9Commented:
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); }
}
dberner9Commented:
You can put your specific code for when the file changes in the if(modified) clause.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
neokeetAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.