Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中扫描文件夹?_Java_Directory - Fatal编程技术网

如何在Java中扫描文件夹?

如何在Java中扫描文件夹?,java,directory,Java,Directory,我需要扫描Java中的特定文件夹,并能够返回特定类型文件的整数(不仅基于扩展名,还基于命名约定)。例如,我想知道\src文件夹中有多少JPG文件具有简单的整数文件名(例如,1.JPG到30.JPG)。谁能给我指出正确的方向吗?Thx是您正在寻找的方法。我有一种方法,它使用正则表达式模式来处理相当复杂的文件结构。类似的东西可以使用,尽管我确信它可以比我的示例(为了安全而编辑)写得更简洁 /** *从给定的foo/flat目录获取所有非目录文件名 * *@param网络 *@param-typeR

我需要扫描Java中的特定文件夹,并能够返回特定类型文件的整数(不仅基于扩展名,还基于命名约定)。例如,我想知道\src文件夹中有多少JPG文件具有简单的整数文件名(例如,1.JPG到30.JPG)。谁能给我指出正确的方向吗?Thx是您正在寻找的方法。

我有一种方法,它使用正则表达式模式来处理相当复杂的文件结构。类似的东西可以使用,尽管我确信它可以比我的示例(为了安全而编辑)写得更简洁

/**
*从给定的foo/flat目录获取所有非目录文件名
* 
*@param网络
*@param-typeRegex
*@param locationRegex
*@返回
*/
公共列表getFilteredFilenames(字符串网络、字符串类型regex、字符串位置regex){
字符串regex=null;
列表文件名=新的ArrayList();
字符串目录;
//看看网络
if(network.equalsIgnoreCase(“foo”)){
//首先获取foo文件
directory=this.pathname+“/”+“foo/filtered/flat”;
File[]foofiles=getFilenames(目录);
//如果需要,运行正则表达式。
if(locationRegex!=null&&typeRegex!=null){
正则表达式=类型正则表达式+“+”位置正则表达式;
//System.out.println(regex);
}
for(int i=0;i
/**
     * Get all non-directory filenames from a given foo/flat directory
     * 
     * @param network
     * @param typeRegex
     * @param locationRegex
     * @return
     */
    public List<String> getFilteredFilenames(String network, String typeRegex, String locationRegex) {

        String regex = null;

        List<String> filenames = new ArrayList<String>();
        String directory;

        // Look at the something network
        if (network.equalsIgnoreCase("foo")) {

            // Get the foo files first
            directory = this.pathname + "/" + "foo/filtered/flat";
            File[] foofiles = getFilenames(directory);

            // run the regex if need be.
            if (locationRegex != null && typeRegex != null ) {
                regex = typeRegex + "." + locationRegex;

                //System.out.println(regex);
            }


            for (int i = 0; i < foofiles.length; i++) {
                if (foofiles[i].isFile()) {
                    String file = foofiles[i].getName();

                    if (regex == null) {
                        filenames.add(file);
                    }
                    else {
                        if (file.matches(regex)) {
                            filenames.add(file);
                        }
                    }
                }
            }
        }

        return filenames;
    }