Reputation:
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
Upvotes: 3
Views: 10274
Reputation: 16209
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?
Upvotes: 2
Reputation: 41
If you want accurate times in Windows environment, have a look at this:
Not all file systems can record creation and last access time and not all file systems record them in the same manner. For example, on NT FAT, create time has a resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time has a resolution of 1 day (really, the access date). On NTFS, access time has a resolution of 1 hour. Therefore, the GetFileTime function may not return the same file time information set using the SetFileTime function. Furthermore, FAT records times on disk in local time. However, NTFS records times on disk in UTC. For more information, see File Times.
Upvotes: 0
Reputation: 328556
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.
Upvotes: 2