【发布时间】:2021-05-16 13:23:22
【问题描述】:
我在 Windows 10 Pro 系统上使用 Files.walkFileTree(),并在某些文件上打印异常 stackTrace。我试图弄清楚如何确定哪些文件会在抛出这个异常之前抛出它;到目前为止,我对导致错误的文件都没有兴趣,但我希望程序能够在错误被抛出之前检测到哪些文件会抛出错误。
代码:
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class WindowsFilePlay extends SimpleFileVisitor<Path>
{
public static void say(String s) { System.out.println(s); }
public static void main(String[] args) throws IOException
{
WindowsFilePlay play = new WindowsFilePlay();
play.go();
}
private void go() throws IOException
{
say("For running on a Windows system.");
String dirString = "c:\\users\\ralph\\appdata\\local\\microsoft\\windowsapps\\";
File dirFile = new File(dirString);
Path dirPath = dirFile.toPath();
Files.walkFileTree(dirPath, this);
say("done.");
}
public FileVisitResult visitFile(Path p, BasicFileAttributes bfa) throws IOException
{
say("visiting " + p.toString());
if (bfa.isSymbolicLink()) { say("It's a link"); } else { say("It's not a link"); }
if (p.compareTo(p.toRealPath()) != 0) { say("not a junction"); }
return FileVisitResult.CONTINUE;
}
}
和控制台:
visiting c:\users\ralph\appdata\local\microsoft\windowsapps\GameBarElevatedFT_Alias.exe
It's not a link
Exception in thread "main" java.nio.file.FileSystemException: c:\users\ralph\appdata\local\microsoft\windowsapps\GameBarElevatedFT_Alias.exe: The file cannot be accessed by the system.
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsLinkSupport.getFinalPath(WindowsLinkSupport.java:74)
at sun.nio.fs.WindowsLinkSupport.getRealPath(WindowsLinkSupport.java:242)
at sun.nio.fs.WindowsPath.toRealPath(WindowsPath.java:836)
at sun.nio.fs.WindowsPath$WindowsPathWithAttributes.toRealPath(WindowsPath.java:138)
at WindowsFilePlay.visitFile(WindowsFilePlay.java:32)
at WindowsFilePlay.visitFile(WindowsFilePlay.java:1)
at java.nio.file.Files.walkFileTree(Files.java:2670)
at java.nio.file.Files.walkFileTree(Files.java:2742)
at WindowsFilePlay.go(WindowsFilePlay.java:24)
at WindowsFilePlay.main(WindowsFilePlay.java:15)
一些进一步的信息:
-
在调试器中,文件属性的类是
sun.nio.fs.WindowsFileAttributes,是BasicFileAttributes的扩展名。我不能只导入 WindowsFileAttributes;它可用于运行时,但我不知道使用的是什么版本,所以我知道要在我的构建路径中放入什么 jar。所以我不能将我的 BasicFileAttributes 变量转换为 WindowsFileAttributes 并调用它的特定方法,除非我能得到官方保证使用哪个 jar。我注意到在该类的调试器中显示了一个名为reparseTag的变量,它看起来像一个位掩码(值为 0x80000001b),但我不知道如何使用它。对于我查看过的其他几个文件,该值为 0。 -
如果我转到DOS命令行,这些文件都在那里;它们的长度为 0,但磁盘上长度为 0 的其他文件被正确处理。在 DOS 中,它们也出现在使用
dir/al生成的列表中,表明(再次)它们是符号链接或重解析点或其他东西。我确信这是相关的,但想知道我的程序如何检测到它们不能像其他文件一样对待。
【问题讨论】: