我使用BasicFileAttributes在Windows中获取Java中的属性,如下所示:
public class Test {
public static void main(String[] args) throws Exception {
BasicFileAttributes attr = null;
Path path = Paths.get("some","file.txt");
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println(attr);
}
}
上面的输出类似于:
sun.nio.fs.WindowsFileAttributes@a1234567
因此我知道 readAttributes 返回一个 WindowsFileAttributes 对象。现在我想将 BasicFileAttributes 转换为 WindowsFileAttributes 并使用它的方法。但我无法导入和使用 WindowsFileAttributes。我收到如下错误:
java: package sun.nio.fs is not visible
(package sun.nio.fs is declared in module java.base, which does not export it to the unnamed module)
如何修复此错误?
最佳答案
无法使用 sun.* 内的类。
一种方法是使用 JNA 访问 native Windows 函数,如下所述:Get unique file id in Windows with Java?
关于java - 无法导入 WindowsFileAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48258290/