这是一个临时解决方案,可以改进以使用Java 8 Streams和Lambdas。
int[] count = {0};try { Files.walkFileTree( Paths.get(dir.getPath()), new HashSet<FileVisitOption>(Arrays.asList(FileVisitOption.FOLLOW_linkS)), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.printf("Visiting file %sn", file); ++count[0]; return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException { System.err.printf("Visiting failed for %sn", file); return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { System.out.printf("about to visit directory %sn", dir); return FileVisitResult.CONTINUE; } });} catch (IOException e) { // handle exception}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)