Java Recursive List recursiveListPartialPaths(File parent, boolean includeDirs)

Here you can find the source of recursiveListPartialPaths(File parent, boolean includeDirs)

Description

Recursively lists a directory, returning the partial paths of the child files.

License

Apache License

Parameter

Parameter Description
parent the directory to start from
includeDirs whether or not to include directories in the results

Return

all partial paths descending from the parent file

Declaration

public static SortedSet<String> recursiveListPartialPaths(File parent, boolean includeDirs) 

Method Source Code

//package com.java2s;
/*//from  w  w  w . j a v a 2s.  c o m
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;

import java.util.Arrays;

import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class Main {
    /**
     * Recursively lists a directory, returning the partial paths of the child
     * files.
     *
     * @param parent the directory to start from
     * @param includeDirs whether or not to include directories in the results
     * @return all partial paths descending from the parent file
     */
    public static SortedSet<String> recursiveListPartialPaths(File parent, boolean includeDirs) {
        assert parent != null;
        TreeSet<String> toReturn = new TreeSet<String>();
        int start = parent.getAbsolutePath().length() + 1;

        List<File> q = new LinkedList<File>();
        q.add(parent);

        while (!q.isEmpty()) {
            File f = q.remove(0);

            if (f.isDirectory()) {
                if (includeDirs) {
                    toReturn.add(f.getAbsolutePath().substring(start));
                }
                q.addAll(Arrays.asList(f.listFiles()));
            } else {
                toReturn.add(f.getAbsolutePath().substring(start));
            }
        }
        return toReturn;
    }
}

Related

  1. recursiveListFiles(File dir, FileFilter filter)
  2. recursiveListFiles(File file, List files)
  3. recursiveListFiles(String path)
  4. recursiveListFilesHelper(File dir, FileFilter filter, List fileList)
  5. recursiveListint(List list, File file, FilenameFilter filter)