Home  >  Article  >  Java  >  A small example of java using recursion to obtain all file paths in a directory

A small example of java using recursion to obtain all file paths in a directory

高洛峰
高洛峰Original
2017-01-17 11:40:411561browse

private List<String> ergodic(File file,List<String> resultFileName){
        File[] files = file.listFiles();
        if(files==null)return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if(f.isDirectory()){// 判断是否文件夹
                resultFileName.add(f.getPath());
                ergodic(f,resultFileName);// 调用自身,查找子目录
            }else
                resultFileName.add(f.getPath());
        }
        return resultFileName;
    }

When calling, use: return ergodic(new File(forderPath), resultList);
The returned result is all the file paths in the directory including subdirectories, including subdirectories of subdirectories...

For more java small examples of using recursion to obtain all file paths in a directory, please pay attention to the PHP Chinese website for related articles!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn