jdk/test/java/nio/file/Files/walkFileTree/PrintFileTree.java
changeset 45505 ca0e16b2d5d6
parent 45504 ea7475564d07
parent 45495 8f5dd0fb0a6d
child 45506 790c716da86b
equal deleted inserted replaced
45504:ea7475564d07 45505:ca0e16b2d5d6
     1 /*
       
     2  * Copyright (c) 2008, 2011, 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 follow = followLinks;
       
    58         final boolean reportCycles = printCycles;
       
    59         Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<Path>() {
       
    60             @Override
       
    61             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
       
    62                 System.out.println(dir);
       
    63                 return FileVisitResult.CONTINUE;
       
    64             }
       
    65             @Override
       
    66             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
       
    67                 System.out.println(file);
       
    68                 return FileVisitResult.CONTINUE;
       
    69             }
       
    70             @Override
       
    71             public FileVisitResult postVisitDirectory(Path 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(Path file, IOException exc)
       
    80                 throws IOException
       
    81             {
       
    82                 if (follow && (exc instanceof FileSystemLoopException)) {
       
    83                     if (reportCycles)
       
    84                         System.out.println(file);
       
    85                     return FileVisitResult.CONTINUE;
       
    86                 } else {
       
    87                     throw exc;
       
    88                 }
       
    89             }
       
    90         });
       
    91     }
       
    92 }