Java 如何使用正则表达式在排除子目录的情况下获取目录中的所有文件?

Java 如何使用正则表达式在排除子目录的情况下获取目录中的所有文件?,java,regex,Java,Regex,关于Java代码: 有人能帮我用正则表达式获取一个目录中的所有文件,而不包括子目录吗 我用过(.)。要匹配目录中的所有文件,但也要搜索我不想搜索的子目录,请使用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; ... /**

关于Java代码: 有人能帮我用正则表达式获取一个目录中的所有文件,而不包括子目录吗


我用过(.)。要匹配目录中的所有文件,但也要搜索我不想搜索的子目录,请使用
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);
}
样本使用
您想使用
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);
}
样本使用
find-键入f-maxdepth 1
我猜这必须在linux环境中的某种shell脚本中完成?请具体说明您计划如何执行此操作。您可以使用tree命令,或者只使用grep.hi Avinash的普通ls,我需要在java代码中使用这个exp作为文件模式的输入。你能推荐我吗way@techie4342始终首先指定正在运行的lang..regexp无法区分文件和目录。使用
File.isDirectory()
find-键入f-maxdepth 1
我猜这必须在linux环境中的某种shell脚本中完成?请具体说明您计划如何执行此操作。您可以使用tree命令,或者只使用grep.hi Avinash的普通ls,我需要在java代码中使用这个exp作为文件模式的输入。你能推荐我吗way@techie4342始终首先指定正在运行的lang..regexp无法区分文件和目录。使用
File.isDirectory()