Java 遍历文件夹

Java About 1,247 words

Files.walkFileTree

preVisitDirectory:进入文件夹前的回调方法。

visitFile:遍历到具体文件的回调方法。

postVisitDirectory:退出文件夹后的回调方法。

visitFileFailed:访问文件失败的回调方法。

public class FilesWalkTreeDemo {

    public static void main(String[] args) throws IOException {
        Files.walkFileTree(Paths.get("./"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("===> " + dir.toString());
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file.toString());
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return super.postVisitDirectory(dir, exc);
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return super.visitFileFailed(file, exc);
            }
        });
    }

}
Views: 1,727 · Posted: 2021-12-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh