filetraverser/FileTraverser.java
changeset 151 a8c2649cf277
equal deleted inserted replaced
150:75133486ba7e 151:a8c2649cf277
       
     1 /**
       
     2  * FileTraverser.java
       
     3  * Copyright (C) 2010 Markus Broeker
       
     4  */
       
     5 
       
     6 import java.io.File;
       
     7 import java.util.List;
       
     8 import java.util.ArrayList;
       
     9 
       
    10 public class FileTraverser {
       
    11 
       
    12     /**
       
    13      * @Params folder: the starting directory, the starting folder
       
    14      * @Params recursive: list subfolders recursively or not
       
    15      * @Params list: the list to store the data
       
    16      */
       
    17     public static void find(String folder, boolean recursive, List<String> list) {
       
    18 
       
    19         File directory = new File (folder);
       
    20         if (!directory.isDirectory())
       
    21             return;
       
    22         else
       
    23             list.add(directory.getPath());
       
    24 
       
    25         /* prevent linux recursion problems when using symlinks */
       
    26         if (Unix.isLink(folder)) {
       
    27             return;
       
    28         }
       
    29 
       
    30         File[] files = directory.listFiles();
       
    31 
       
    32         for (File f : files) {
       
    33             if (f.isDirectory() && recursive) {
       
    34                 find(f.getPath(), recursive, list);
       
    35             } else
       
    36                 list.add(f.getPath());
       
    37         }
       
    38     }
       
    39 
       
    40     public static void main(String[] args) {
       
    41         List<String> list = new ArrayList<String>();
       
    42         String homeDir = System.getProperty("user.home");
       
    43         boolean recursive = false;
       
    44 
       
    45         if (args.length == 2) {
       
    46             homeDir = args[0];
       
    47             recursive = Integer.parseInt(args[1]) != 0 ? true : false;
       
    48         }
       
    49 
       
    50         find(homeDir, recursive, list);
       
    51         for (String s : list) {
       
    52             System.out.println ("List: " + s);
       
    53         }
       
    54     }
       
    55 }