java.io.File.lastModified 对于链接,获取链接文件的lastModified,而不是链接文件

标签 java

$ touch file1.txt; sleep 60; ln -s file1.txt file2.txt; ls -l
$ -rw-r--r--   1 david  staff    0 Mar 29 22:58 file1.txt
$ lrwxr-xr-x   1 david  staff    9 Mar 29 22:59 file2.txt -> file1.txt

但是,这两个语句返回 file1.txt 的时间戳:

new java.io.File("file1.txt").lastModified()  // Mar 29 22:58
new java.io.File("file2.txt").lastModified()  // Mar 29 22:58

所以,file2.txt正在获取lastModified来自其链接文件的信息。

java.nio.file.Files有同样的问题:

Files.readAttributes(new java.io.File("file2.txt").toPath(), classOf[PosixFileAttributes]).lastModifiedTime.toMillis

所以,在Java/Scala中,当我有一个链接文件时,如何获取 lastModifiedTime (和 Files.readAttributes.permissions )链接文件本身(而不是链接文件),如 ls -l 中所示命令?

最佳答案

java.nio.file.Files 的 Javadoc:

public static FileTime getLastModifiedTime(Path path,
                                       LinkOption... options)
                                throws IOException

Returns a file's last modified time.

The options array may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed and the file attribute of the final target of the link is read. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.

关于java.io.File.lastModified 对于链接,获取链接文件的lastModified,而不是链接文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295064/

相关文章:

java 带线程的方法返回类型

java - Java 查询中的 SQL 变量

java - 需要是 LocalDate 的日期对象的正确类型

java - SQLite 错误代码 = 1

java - JOptionPane 消息显示 null

java - Json 到 avro 的转换

java - Android Studio Robolectric 单元测试 - 默认值无效

java - 类型不匹配 : cannot convert from Object to AccountDAO

java - 错误: reached end of file while parsing

Java正则表达式,匹配以x开头、以y结尾的段落