Friday, August 5, 2016

How to check if file is modified or not in JAVA

Hi Guys,

Below program is use to track the changes in the file.


package com.vks.example;

import java.io.File;

public class FileModifed {

  private static long lastModified = 0L;


static {

lastModified=new File("C://example.txt").lastModified();
  }
public static void main(String[] args) {

//calling isFileModied method in loop to check if file is modified or not.A infinite for loop to check file is modified or not.
  for (int i = 0; i >= 0; i++) {

if (isFileModified())
break;
  }

}

public static boolean isFileModified() {

  File file = new File("C://example.txt");
  if (lastModified >= file.lastModified()) {
  System.out.println(lastModified + "-------- Not Modified-------" + file.lastModified());
return false;
  } else {
lastModified = file.lastModified();
System.out.println("-------- Modified-------");
return true;
  }
    }

}

Any comments or Suggestion are most welcome!!

Thanks,
Vikas

No comments:

Post a Comment