java.nio.file.AccessDeniedException: F:System Volume Information
1. 處理目錄
Path dir = Paths.get("F:");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.txt")) {
for (Path entry : stream) {
System.out.println(entry.getFileName());
}
} catch (IOException e) {
e.printStackTrace();
}
2. 處理目錄樹
Path dir = Paths.get("F:");
try {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.toString().endsWith(".pptx")) {
System.out.println(file.toAbsolutePath());
return FileVisitResult.CONTINUE;
}
//這做了過濾還報錯
return FileVisitResult.SKIP_SUBTREE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
3. 處理目錄樹報錯以下:
java.nio.file.AccessDeniedException: F:System Volume Information
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:518)
at java.nio.file.Files.newDirectoryStream(Files.java:457)
at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
at java.nio.file.Files.walkFileTree(Files.java:2706)
at java.nio.file.Files.walkFileTree(Files.java:2742)
at net.liuzd.java.demo.one.DirectoryStreamMain.main(DirectoryStreamMain.java:21)
Path dir = Paths.get("F:");
try {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.toString().endsWith(".pptx")) {
System.out.println(file.toAbsolutePath());
return FileVisitResult.CONTINUE;
}
return FileVisitResult.SKIP_SUBTREE;
}
//增長失敗處理,直接過濾
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
if (exc instanceof AccessDeniedException) {
return FileVisitResult.SKIP_SUBTREE;
}
return super.visitFileFailed(file, exc);
}
});
} catch (IOException e) {
e.printStackTrace();
}