filetraverser/FileTraverser.java
author Markus Bröker<broeker.markus@googlemail.com>
Sat, 21 Oct 2017 13:45:05 +0200
changeset 171 c6e0af68825a
parent 151 a8c2649cf277
permissions -rw-r--r--
Entrypoint and RET fixed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
151
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * FileTraverser.java
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2010 Markus Broeker
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
import java.io.File;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
import java.util.List;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
import java.util.ArrayList;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
public class FileTraverser {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
    /**
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
     * @Params folder: the starting directory, the starting folder
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
     * @Params recursive: list subfolders recursively or not
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
     * @Params list: the list to store the data
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
     */
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    public static void find(String folder, boolean recursive, List<String> list) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
        File directory = new File (folder);
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
        if (!directory.isDirectory())
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
            return;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
        else
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
            list.add(directory.getPath());
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        /* prevent linux recursion problems when using symlinks */
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        if (Unix.isLink(folder)) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
            return;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        File[] files = directory.listFiles();
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        for (File f : files) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
            if (f.isDirectory() && recursive) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
                find(f.getPath(), recursive, list);
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
            } else
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
                list.add(f.getPath());
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    public static void main(String[] args) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        List<String> list = new ArrayList<String>();
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        String homeDir = System.getProperty("user.home");
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        boolean recursive = false;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        if (args.length == 2) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
            homeDir = args[0];
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
            recursive = Integer.parseInt(args[1]) != 0 ? true : false;
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
        find(homeDir, recursive, list);
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        for (String s : list) {
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
            System.out.println ("List: " + s);
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    }
a8c2649cf277 FileTraverser Demo added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
}