jdk/test/java/nio/file/Files/PrintFileTree.java
changeset 2057 3acf8e5e2ca0
child 4976 7419a22f407c
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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 -L option causes symbolic
       
    32  * links to be followed.
       
    33  */
       
    34 
       
    35 public class PrintFileTree {
       
    36 
       
    37     public static void main(String[] args) throws Exception {
       
    38         boolean followLinks = false;
       
    39         Path dir;
       
    40 
       
    41         if (args[0].equals("-L")) {
       
    42             followLinks = true;
       
    43             dir = Paths.get(args[1]);
       
    44         } else {
       
    45             dir = Paths.get(args[0]);
       
    46         }
       
    47 
       
    48         Set<FileVisitOption> options = new HashSet<FileVisitOption>();
       
    49         if (followLinks)
       
    50             options.add(FileVisitOption.FOLLOW_LINKS);
       
    51 
       
    52         Files.walkFileTree(dir, options, Integer.MAX_VALUE, new FileVisitor<FileRef>() {
       
    53             public FileVisitResult preVisitDirectory(FileRef dir) {
       
    54                 System.out.println(dir);
       
    55                 return FileVisitResult.CONTINUE;
       
    56             }
       
    57             public FileVisitResult preVisitDirectoryFailed(FileRef dir, IOException exc) {
       
    58                 exc.printStackTrace();
       
    59                 return FileVisitResult.CONTINUE;
       
    60             }
       
    61             public FileVisitResult visitFile(FileRef file, BasicFileAttributes attrs) {
       
    62                 System.out.println(file);
       
    63                 return FileVisitResult.CONTINUE;
       
    64             }
       
    65             public FileVisitResult postVisitDirectory(FileRef dir, IOException exc) {
       
    66                 if (exc != null) {
       
    67                     exc.printStackTrace();
       
    68                     return FileVisitResult.TERMINATE;
       
    69                 }
       
    70                 return FileVisitResult.CONTINUE;
       
    71             }
       
    72             public FileVisitResult visitFileFailed(FileRef file, IOException exc) {
       
    73                 exc.printStackTrace();
       
    74                 return FileVisitResult.TERMINATE;
       
    75             }
       
    76         });
       
    77     }
       
    78 }