Here you can find the source of getFilesAndFilesSubDirectories(String directoryName, String regexPattern)
Parameter | Description |
---|---|
directoryName | a parameter |
regexPattern | a parameter |
private static List<File> getFilesAndFilesSubDirectories(String directoryName, String regexPattern)
//package com.java2s; //License from project: LGPL import java.io.File; import java.util.ArrayList; import java.util.List; public class Main { public static final String allFilterRegex = ".*"; /**/*from w w w . j ava 2 s .c o m*/ * * @param directoryName * @param regexPattern * @return */ private static List<File> getFilesAndFilesSubDirectories(String directoryName, String regexPattern) { File directory = new File(directoryName); //get all the files from a directory File[] fList = directory.listFiles(); List<File> fileList = new ArrayList<File>(); if (fList == null) { return fileList; } for (File file : fList) { if (!file.getName().matches(regexPattern)) { continue; } fileList.add(file); if (file.isDirectory()) { getFilesAndFilesSubDirectories(file.getAbsolutePath(), regexPattern); } } return fileList; } /** * list folders and file by String pattern * example directoryName= /home/MyUser/Dekstop/myfiles/ * pattern=* * @param directoryName to be listed */ public static List<File> listFiles(String directoryName) { return listFilesByRegex(directoryName, allFilterRegex); } /** * * @param directoryName * @param pattern * @return */ public static List<File> listFiles(String directoryName, String pattern) { String regexPattern = pattern == null ? allFilterRegex : pattern.replace(".", "\\.").replace("*", ".*"); return listFilesByRegex(directoryName, regexPattern); } /** * list files by regex Pattern * example directoryName= /home/MyUser/Dekstop/myfiles/ * example pattern =/.*.json/ * * @param directoryName * @param regexPattern * @return */ public static List<File> listFilesByRegex(String directoryName, String regexPattern) { File directory = new File(directoryName); //get all the files from a directory File[] fList = directory.listFiles(); List<File> fileList = new ArrayList<File>(); if (fList == null) { return fileList; } for (File file : fList) { if (file.isFile() && file.getName().matches(regexPattern)) { fileList.add(file); } } return fileList; } }