简体   繁体   English

如何使用正则表达式获取目录中不包含子目录的所有文件?

[英]How to get all files in a directory with exclusion of subdirectories using regular expression?

Relating to Java code: can any one help me to get all files in a directory with exclusion of sub directories using regular expressions 与Java代码有关:谁能帮我使用正则表达式获取目录中的所有文件,但不包括子目录

I have used (. ). 我用过(。 )。 to match all files in directory but it also searching subdirectories that I don't want to 匹配目录中的所有文件,但也搜索我不想的子目录

You want to use a java.io.FileFilter : 您要使用java.io.FileFilter

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

...


/**
 * 
 * Search in {@code directory} all files excluding or 
 * including subdirectories matching given {@code regex}.
 * 
 * @param directory The directory where the search is done.
 * @param regex The regex used to match the directories to exclude or include.
 * @param exclude Indicate if the matched directories will be excluded (TRUE) or included (FALSE).
 * 
 * @return All files including or excluding directories matched by {@code regex}. 
 *
 */
public static File[] getAllFiles(String directory, final String regex, final boolean exclude)  {
    File f = new File(directory);

    FileFilter regexFilter = new FileFilter() {
        private Matcher REGEX_FILTER = Pattern.compile(regex).matcher("");

        public boolean accept(File pathname) {
            boolean isDirectory = pathname.isDirectory();

            if (isDirectory && REGEX_FILTER.reset(pathname.getName()).matches()) {
                return !exclude;
            }

            if (isDirectory) {
                return false;
            }

            return true;
        }
    };

    return f.listFiles(regexFilter);
}

Sample usage 样品用法

// Show all files in current directory with exclusion of subdirectories...
// ... having a name starting with a dot.
for (File file : getAllFiles(".", "^\\..+$")) {
    if (file.isDirectory()) {
        System.out.print("directory:");
    } else {
        System.out.print("     file:");
    }
    System.out.println(file.getCanonicalPath());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 FTP 上的骆驼路线将所有文件从目录(包括子目录)移动到目标中没有子目录的特定目录? - How to move all files from a directory(including subdirectories) to a specific directory without subdirectories in target using camel route over FTP? 如何使用MediaStore获取特定目录和所有子目录中所有音乐的列表 - How to get list of all music in specific directory and all subdirectories using MediaStore 如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录 - How to use Java's directory stream to get files/subdirectories only within directory not other subdirectories 如何在hadoop hdfs中列出目录及其子目录中的所有文件 - How to list all files in a directory and its subdirectories in hadoop hdfs 快速列出目录及其所有子目录中的文件 - Fast listing files in a directory and all of its subdirectories 如何获取不包括某些子目录的所有子目录? - How to get all subdirectories excluding some subdirectories? 获取目录及其子目录中的文件数 - get number of files in a directory and its subdirectories 如何使用Java对文件夹及其所有文件和子目录进行zip / upzip? - How to zip/upzip a folder and all of its files and subdirectories using Java? 以递归方式获取Java中的目录及其子目录中的所有文件 - Non-recursive way to get all files in a directory and its subdirectories in Java Java-递归获取目录和子目录中的所有文件 - Java - Get all files in directories and subdirectories recursively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM