如何在 Kotlin 中获取文件系统元数据?

How to get file system meta-data in Kotlin?

我正在编写一个程序,它应该输出指定目录中所有文件的元信息(大小、执行/读/写权限、上次修改时间)。

所以我从 java 中找到了 class,命名为 BasicFileAttributes (link)。 那里描述了调用该方法所需的所有参数。然而,我的IDE给出了错误,

None of the following functions can be called with arguments supplied

...用红色路径突出显示。我不明白我在哪里犯了一个愚蠢的错误。我将非常感谢您的建议

val path = readLine().toString()
val attr = Files.readAttributes("img.png", BasicFileAttributes.class)

或者也不行

val path = readLine().toString()
val attr = Files.readAttributes(path, BasicFileAttributes.class)

Files.readAttributesPath 作为其第一个参数,而不是 String。此外,您需要传递 Java Class 类型,而不是 KClass 类型。你想要这样的东西:

val path = FileSystems.getDefault().getPath(readLine())
val attr = Files.readAttributes<BasicFileAttributes>(path, BasicFileAttributes::class.java)