Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Files/walkFileTree/PrintFileTree.java
38886 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.*;24import java.nio.file.attribute.*;25import java.io.IOException;26import java.util.*;2728/**29* Invokes Files.walkFileTree to traverse a file tree and prints30* each of the directories and files. The -follow option causes symbolic31* links to be followed and the -printCycles option will print links32* where the target of the link is an ancestor directory.33*/3435public class PrintFileTree {3637public static void main(String[] args) throws Exception {38boolean followLinks = false;39boolean printCycles = false;40int i = 0;41while (i < (args.length-1)) {42switch (args[i]) {43case "-follow" : followLinks = true; break;44case "-printCycles" : printCycles = true; break;45default:46throw new RuntimeException(args[i] + " not recognized");47}48i++;49}50Path dir = Paths.get(args[i]);5152Set<FileVisitOption> options = new HashSet<FileVisitOption>();53if (followLinks)54options.add(FileVisitOption.FOLLOW_LINKS);5556final boolean follow = followLinks;57final boolean reportCycles = printCycles;58Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<Path>() {59@Override60public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {61System.out.println(dir);62return FileVisitResult.CONTINUE;63}64@Override65public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {66System.out.println(file);67return FileVisitResult.CONTINUE;68}69@Override70public FileVisitResult postVisitDirectory(Path dir, IOException exc)71throws IOException72{73if (exc != null)74throw exc;75return FileVisitResult.CONTINUE;76}77@Override78public FileVisitResult visitFileFailed(Path file, IOException exc)79throws IOException80{81if (follow && (exc instanceof FileSystemLoopException)) {82if (reportCycles)83System.out.println(file);84return FileVisitResult.CONTINUE;85} else {86throw exc;87}88}89});90}91}929394