filetraverser/FileTraverser.java
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 151 a8c2649cf277
permissions -rw-r--r--
Proper Datatypes to leave the 80's

/**
 * FileTraverser.java
 * Copyright (C) 2010 Markus Broeker
 */

import java.io.File;
import java.util.List;
import java.util.ArrayList;

public class FileTraverser {

    /**
     * @Params folder: the starting directory, the starting folder
     * @Params recursive: list subfolders recursively or not
     * @Params list: the list to store the data
     */
    public static void find(String folder, boolean recursive, List<String> list) {

        File directory = new File (folder);
        if (!directory.isDirectory())
            return;
        else
            list.add(directory.getPath());

        /* prevent linux recursion problems when using symlinks */
        if (Unix.isLink(folder)) {
            return;
        }

        File[] files = directory.listFiles();

        for (File f : files) {
            if (f.isDirectory() && recursive) {
                find(f.getPath(), recursive, list);
            } else
                list.add(f.getPath());
        }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        String homeDir = System.getProperty("user.home");
        boolean recursive = false;

        if (args.length == 2) {
            homeDir = args[0];
            recursive = Integer.parseInt(args[1]) != 0 ? true : false;
        }

        find(homeDir, recursive, list);
        for (String s : list) {
            System.out.println ("List: " + s);
        }
    }
}