java 获取文件修改时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1518704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:52:56  来源:igfitidea点击:

get file modification time

java

提问by

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

我想编写一个代码来监视文件更改并对更改做出反应。所以我写了一个 TimerTask 来定期检查文件的修改,但我有一个问题:当文件被其他程序打开时,比如 excel 或 word 并且我没有任何更改地关闭文件时,值 File.lastModified() 正在更改。我也试图通过运行 dir shell 脚本来获取修改日期,它工作正常,但它只有微小的准确性!谁能帮我?谢谢

回答by Aaron Digulla

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.

当您打开文件时,Word 和 Excel 会将您的用户名写入文件(以便其他用户在尝试打开文件时可以看到谁在处理该文件)。所以它File.lastModified()正在改变是正确的。

On Windows, there is no command line tool which can show you the modification time in seconds.

在 Windows 上,没有命令行工具可以以秒为单位显示修改时间。

回答by Adriaan Koster

I don't see the behavior you describe, using OpenOffice under Windows.

我没有看到您描述的行为,在 Windows 下使用 OpenOffice。

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?

也许你可以发布一些代码来展示你是如何做的?你在什么平台上运行你的应用程序?

回答by Rope

If you want accurate times in Windows environment, have a look at this:

如果您想在 Windows 环境中获得准确的时间,请查看以下内容:

WinAPI FILETIME structure

WinAPI 文件时间结构

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.

并非所有文件系统都可以记录创建和上次访问时间,也并非所有文件系统都以相同的方式记录它们。例如,在 NT FAT 上,创建时间的分辨率为 10 毫秒,写入时间的分辨率为 2 秒,访问时间的分辨率为 1 天(实际上是访问日期)。在 NTFS 上,访问时间的分辨率为 1 小时。因此,GetFileTime 函数可能不会返回使用 SetFileTime 函数设置的相同文件时间信息。此外,FAT 以本地时间在磁盘上记录时间。但是,NTFS 在磁盘上以 UTC 时间记录时间。有关更多信息,请参阅文件时间。