893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/A/**
893N/A * Invokes Files.walkFileTree to traverse a file tree and prints
2188N/A * each of the directories and files. The -follow option causes symbolic
2188N/A * links to be followed and the -printCycles option will print links
2188N/A * where the target of the link is an ancestor directory.
893N/A */
893N/A
893N/Apublic class PrintFileTree {
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A boolean followLinks = false;
2188N/A boolean printCycles = false;
2188N/A int i = 0;
2188N/A while (i < (args.length-1)) {
2188N/A switch (args[i]) {
2188N/A case "-follow" : followLinks = true; break;
2188N/A case "-printCycles" : printCycles = true; break;
2188N/A default:
2188N/A throw new RuntimeException(args[i] + " not recognized");
2188N/A }
2188N/A i++;
893N/A }
2188N/A Path dir = Paths.get(args[i]);
893N/A
893N/A Set<FileVisitOption> options = new HashSet<FileVisitOption>();
893N/A if (followLinks)
893N/A options.add(FileVisitOption.FOLLOW_LINKS);
893N/A
3574N/A final boolean follow = followLinks;
2188N/A final boolean reportCycles = printCycles;
3471N/A Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<Path>() {
2826N/A @Override
3471N/A public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
893N/A System.out.println(dir);
893N/A return FileVisitResult.CONTINUE;
893N/A }
2826N/A @Override
3471N/A public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
3574N/A System.out.println(file);
893N/A return FileVisitResult.CONTINUE;
893N/A }
2826N/A @Override
3471N/A public FileVisitResult postVisitDirectory(Path dir, IOException exc)
2826N/A throws IOException
2826N/A {
2826N/A if (exc != null)
2826N/A throw exc;
893N/A return FileVisitResult.CONTINUE;
893N/A }
2826N/A @Override
3471N/A public FileVisitResult visitFileFailed(Path file, IOException exc)
2826N/A throws IOException
2826N/A {
3574N/A if (follow && (exc instanceof FileSystemLoopException)) {
3574N/A if (reportCycles)
3574N/A System.out.println(file);
2826N/A return FileVisitResult.CONTINUE;
3574N/A } else {
3574N/A throw exc;
2826N/A }
893N/A }
893N/A });
893N/A }
893N/A}