Reputation: 9
I have written a piece of code to compare time stamp to check for file modification
Here is the code
private long timeStamp;
private File file;
private boolean isFileUpdated(File file)
{
this.file = file;
this.timeStamp = file.lastModified();
if (this.timeStamp != timeStamp)
{
this.timeStamp = timeStamp;
// Yes, file is updated
return true;
}
// No, file is not updated
return false;
}
public static void main(String[] args)
{
Test test=new Test();
File testFile=new File("D:/Test/Test.txt");
boolean result=test.isFileUpdated(testFile);
System.out.println("the result is " + result);
}
The problem with this code is it doesn't maintain any history of timestamp. So do I need to place the timestamp in a txt file so that it reads from the txt file and compares it will current lastmodifieddate and if there is change that means a file has been modified?
Upvotes: 0
Views: 234
Reputation: 37506
This will write your history for you:
PrintWriter writer = new PrintWriter(FileWriter(new File("log.log"), true));
//true for append mode
writer.println(file.lastModified());
writer.close();
You can read it in with a Scanner like this
Scanner s = new Scanner(new File("log.log"));
String line = null;
while (s.hasNextLine()) {
line = s.nextLine();
}
s.close();
//line is the last line read
Upvotes: 1