Get file modification time

I want to write code to monitor file changes and respond to changes. therefore, I write TimerTask to periodically check for changes to the file, but I have a problem: when the file is opened by other programs, such as excel or word, and I close the file without any changes, the value of File.lastModified () changes. I am also trying to get the modification date by running the dir shell script, it works fine, but it only has minute accuracy! Can someone help me? thanks

+3
source share
4 answers

Word and Excel write your username to a file when it is opened (so that other users can see who is working on it when they try to open the file). Therefore, he corrects the change File.lastModified().

Windows does not have a command line tool that can show you the modification time in seconds.

+2
source

I do not see the behavior you described using OpenOffice on 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()));
    }
}

exit:

Before: Mon Oct 05 10:01:27 CEST 2009

After: Mon Oct 05 10:01:27 CEST 2009

Maybe you can post a code showing how you do it? And on which platform do you run your application?

+2
source

.NET FileSystemWatcher. Java. FileSystemWatcher Java

0

Windows, :

WinAPI FILETIME

, . , NT FAT 10 , 2 , 1 (, ). NTFS 1 . GetFileTime , SetFileTime. , FAT . , NTFS UTC. . .

0

Source: https://habr.com/ru/post/1719256/


All Articles