遇到异常错误后JavaFiles.walk能否继续执行
Can Java Files.walk continue executing after encountering an exception error
这是我第一次在 Java 中使用 Files.walk,我很喜欢它,尤其是路径 class。它坚固耐用且用途广泛。
然而,我不喜欢的是,当遍历文件夹时,如果迭代器遇到障碍...就像它碰到了用户无权查看的文件夹或可能出现的任何错误遇到,抛出异常后,它不会继续,也不会产生要使用的路径列表。
这是我如何使用该方法的示例:
try {
Stream<Path> paths = Files.walk(rootPath);
List<Path> pathList = paths.collect(Collectors.toList());
for (Path path : pathList) {
//Processing code here
}
}
catch (FileSystemException a) {System.err.format("FileSystemException: %s%n", a);System.out.println(a.getStackTrace());}
catch (UncheckedIOException b) {System.err.format("UncheckedIOException: %s%n", b);System.out.println(b.getCause().getLocalizedMessage());}
catch (IOException c) { System.err.format("IOException: %s%n", c);System.out.println(c.getStackTrace());}
现在,如果 Files.walk 方法在尝试遍历文件夹时遇到某种错误,它会抛出异常,然后代码当然会在捕获异常后继续。
我想让它做的是忽略它遇到的任何问题,并继续不停地遍历它可以遍历的任何文件夹。
这可能吗?
您可以使用 Files#walkFileTree()
方法并实现您自己的访问者对象,它将忽略异常:
public class Test
{
public static void main(String[] args) throws Exception
{
Path rootPath = Path.of("e:\walkMe");
Files.walkFileTree(rootPath,new MyVisitor());
}
}
class MyVisitor implements FileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("error on: " + file + " " + exc.getClass());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
}
输出:
e:\walkMe
e:\walkMe\ab
e:\walkMe\ab\ab.txt
error on: e:\walkMe\cd class java.nio.file.AccessDeniedException
e:\walkMe\ef
e:\walkMe\ef\ef.txt
此处应用程序无法浏览文件夹 cd
,但它会继续浏览其余文件夹和文件。
我们仍然可以像这样使用 Files#walk
:
Path rootPath = Path.of("e:\walkMe");
Iterator<Path> itr = Files.walk(rootPath).iterator();
while(true) {
try {
if(itr.hasNext()) {
System.out.println(itr.next());
} else {
break;
}
}catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
输出:
e:\walkMe
e:\walkMe\ab
e:\walkMe\ab\ab.txt
java.nio.file.AccessDeniedException: e:\walkMe\cd
e:\walkMe\ef
e:\walkMe\ef\ef.txt
这是我第一次在 Java 中使用 Files.walk,我很喜欢它,尤其是路径 class。它坚固耐用且用途广泛。
然而,我不喜欢的是,当遍历文件夹时,如果迭代器遇到障碍...就像它碰到了用户无权查看的文件夹或可能出现的任何错误遇到,抛出异常后,它不会继续,也不会产生要使用的路径列表。
这是我如何使用该方法的示例:
try {
Stream<Path> paths = Files.walk(rootPath);
List<Path> pathList = paths.collect(Collectors.toList());
for (Path path : pathList) {
//Processing code here
}
}
catch (FileSystemException a) {System.err.format("FileSystemException: %s%n", a);System.out.println(a.getStackTrace());}
catch (UncheckedIOException b) {System.err.format("UncheckedIOException: %s%n", b);System.out.println(b.getCause().getLocalizedMessage());}
catch (IOException c) { System.err.format("IOException: %s%n", c);System.out.println(c.getStackTrace());}
现在,如果 Files.walk 方法在尝试遍历文件夹时遇到某种错误,它会抛出异常,然后代码当然会在捕获异常后继续。
我想让它做的是忽略它遇到的任何问题,并继续不停地遍历它可以遍历的任何文件夹。
这可能吗?
您可以使用 Files#walkFileTree()
方法并实现您自己的访问者对象,它将忽略异常:
public class Test
{
public static void main(String[] args) throws Exception
{
Path rootPath = Path.of("e:\walkMe");
Files.walkFileTree(rootPath,new MyVisitor());
}
}
class MyVisitor implements FileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("error on: " + file + " " + exc.getClass());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
}
输出:
e:\walkMe
e:\walkMe\ab
e:\walkMe\ab\ab.txt
error on: e:\walkMe\cd class java.nio.file.AccessDeniedException
e:\walkMe\ef
e:\walkMe\ef\ef.txt
此处应用程序无法浏览文件夹 cd
,但它会继续浏览其余文件夹和文件。
我们仍然可以像这样使用 Files#walk
:
Path rootPath = Path.of("e:\walkMe");
Iterator<Path> itr = Files.walk(rootPath).iterator();
while(true) {
try {
if(itr.hasNext()) {
System.out.println(itr.next());
} else {
break;
}
}catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
输出:
e:\walkMe
e:\walkMe\ab
e:\walkMe\ab\ab.txt
java.nio.file.AccessDeniedException: e:\walkMe\cd
e:\walkMe\ef
e:\walkMe\ef\ef.txt