Home > Java > javaTutorial > 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

高洛峰
Release: 2017-01-17 11:40:41
Original
1576 people have browsed it

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;
    }
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template