$ 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/