i want to write a code for monitoring file changes and reacting to changes. so i write a TimerTask to periodically check the modification of file but i have a problem: when file is open by other programs such as excel or word and i'm closing the file without any changes,value File.lastModified() is changing. i am also trying to get modification date by running dir shell script, it's work fine but it only has minute accuracy! can any one help me? thanks
Word and Excel write your user name into the file when you open it (so other users can see who is working on it when they try to open the file). So it's correct that File.lastModified() is changing.
On Windows, there is no command line tool which can show you the modification time in seconds.
I don't see the behavior you describe, using OpenOffice under Windows.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Scanner;
public class CsvWriter {
public static void main(String args[]) throws IOException {
String fileName = "test.xls";
File file = new File(fileName);
PrintWriter out = new PrintWriter(new FileWriter(file));
out.println("a,b,c,d");
out.println("e,f,g,h");
out.println("i,j,k,l");
out.close();
System.out.println("Before: " + new Date(file.lastModified()));
// manual steps:
// open test.xls with OpenOffice
// close test.xls
// press enter
System.in.read(); // wait until 'enter' is pressed
System.out.println("After: " + new Date(file.lastModified()));
}
}
output:
Before: Mon Oct 05 10:01:27 CEST 2009
After: Mon Oct 05 10:01:27 CEST 2009
Maybe you could post some code showing how you are doing it? And on what platform are you running your application?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With