Java Directory to File List getAllFiles(File directory, String rootPath)

Here you can find the source of getAllFiles(File directory, String rootPath)

Description

Search through the directory and find all sub-folders, files and those in its sub-folders recursively.

License

Open Source License

Parameter

Parameter Description
directory the directory to search through to get files
rootPath the path to use to replace the map key in return result

Return

a map with the key be the relative path of the file to the directory (start with "/") and value be the file

Declaration

public static Map<String, File> getAllFiles(File directory, String rootPath) 

Method Source Code

//package com.java2s;
// distribute, sublicense, and/or sell copies of the Software, and to

import java.io.File;

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**//ww w.j av  a2  s  .co m
     * Search through the directory and find all sub-folders, files and those in 
     * its sub-folders recursively. 
     * This function will skip when there is not enough right to access the folder.
     * @param directory the directory to search through to get files
     * @param rootPath the path to use to replace the map key in return result
     * @return a map with the key be the relative path of the file to the 
     * {@code directory} (start with "/") and value be the file
     */
    public static Map<String, File> getAllFiles(File directory, String rootPath) {
        if (directory == null) {
            throw new NullPointerException("argument 'directory' cannot be null");
        }
        if (rootPath == null) {
            throw new NullPointerException("argument 'rootPath' cannot be null");
        }

        Map<String, File> returnResult = new HashMap<String, File>();

        String fileRootPath = rootPath != null ? rootPath : directory.getAbsolutePath();

        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File _file : files) {
                    if (_file.isHidden()) {
                        continue;
                    }
                    if (_file.isDirectory()) {
                        returnResult.putAll(getAllFiles(_file, fileRootPath));
                    } else {
                        returnResult.put(
                                _file.getAbsolutePath().replace(fileRootPath, "").replace(File.separator, "/"),
                                _file);
                    }
                }
            }
        }
        returnResult.put(directory.getAbsolutePath().replace(fileRootPath, "").replace(File.separator, "/"),
                directory);

        return returnResult;
    }
}

Related

  1. getAllFiles(File dir, List fileList)
  2. getAllFiles(File dir, Pattern filter)
  3. getAllFiles(File directory)
  4. getAllFiles(File directory)
  5. getAllFiles(File directory, boolean hidden)
  6. getAllFiles(File inFileOrDir, boolean recurseDir)
  7. getAllFiles(File input)
  8. getAllFiles(File outputDir, List files)
  9. getAllFiles(File path, List fileList, boolean recursive)