jdk/test/java/nio/file/Files/PrintFileTree.java
changeset 8249 c3d31a2ce7c6
parent 8248 09e47b898040
parent 8219 4a1c655bfb69
child 8250 a36beda9b9de
equal deleted inserted replaced
8248:09e47b898040 8249:c3d31a2ce7c6
     1 /*
       
     2  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.nio.file.*;
       
    25 import java.nio.file.attribute.*;
       
    26 import java.io.IOException;
       
    27 import java.util.*;
       
    28 
       
    29 /**
       
    30  * Invokes Files.walkFileTree to traverse a file tree and prints
       
    31  * each of the directories and files. The -follow option causes symbolic
       
    32  * links to be followed and the -printCycles option will print links
       
    33  * where the target of the link is an ancestor directory.
       
    34  */
       
    35 
       
    36 public class PrintFileTree {
       
    37 
       
    38     public static void main(String[] args) throws Exception {
       
    39         boolean followLinks = false;
       
    40         boolean printCycles = false;
       
    41         int i = 0;
       
    42         while (i < (args.length-1)) {
       
    43             switch (args[i]) {
       
    44                 case "-follow"      : followLinks = true; break;
       
    45                 case "-printCycles" : printCycles = true;  break;
       
    46                 default:
       
    47                     throw new RuntimeException(args[i] + " not recognized");
       
    48             }
       
    49             i++;
       
    50         }
       
    51         Path dir = Paths.get(args[i]);
       
    52 
       
    53         Set<FileVisitOption> options = new HashSet<FileVisitOption>();
       
    54         if (followLinks)
       
    55             options.add(FileVisitOption.FOLLOW_LINKS);
       
    56 
       
    57         final boolean reportCycles = printCycles;
       
    58         Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<FileRef>() {
       
    59             @Override
       
    60             public FileVisitResult preVisitDirectory(FileRef dir, BasicFileAttributes attrs) {
       
    61                 System.out.println(dir);
       
    62                 return FileVisitResult.CONTINUE;
       
    63             }
       
    64             @Override
       
    65             public FileVisitResult visitFile(FileRef file, BasicFileAttributes attrs) {
       
    66                 if (!attrs.isDirectory() || reportCycles)
       
    67                     System.out.println(file);
       
    68                 return FileVisitResult.CONTINUE;
       
    69             }
       
    70             @Override
       
    71             public FileVisitResult postVisitDirectory(FileRef dir, IOException exc)
       
    72                 throws IOException
       
    73             {
       
    74                 if (exc != null)
       
    75                     throw exc;
       
    76                 return FileVisitResult.CONTINUE;
       
    77             }
       
    78             @Override
       
    79             public FileVisitResult visitFileFailed(FileRef file, IOException exc)
       
    80                 throws IOException
       
    81             {
       
    82                 if (reportCycles && (exc instanceof FileSystemLoopException)) {
       
    83                     System.out.println(file);
       
    84                     return FileVisitResult.CONTINUE;
       
    85                 }
       
    86                 throw exc;
       
    87             }
       
    88         });
       
    89     }
       
    90 }