Here you can find the source of getAllFiles(File directory, String rootPath)
Parameter | Description |
---|---|
directory | the directory to search through to get files |
rootPath | the path to use to replace the map key in return result |
public static Map<String, File> getAllFiles(File directory, String rootPath)
//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; } }