how to get name of all file in a directory and sub-directory in java - Stack Overflow icon-default.png?t=M85B https://stackoverflow.com/questions/8788976/how-to-get-name-of-all-file-in-a-directory-and-sub-directory-in-java

pom导入依赖

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

以.java文件为例,指定目录为directory,listFiles第三个参数表示是否递归,递归即读取子目录

import org.apache.commons.io.FileUtils;
import java.io.File;
File rootDir = new File('directory');
Collection<File> files = FileUtils.listFiles(rootDir, new String[]{"java"}, true);
读取 出某个 文件 夹下面的所有某 后缀 名的 文件 String file Path = “某个 目录 ”; File file = new File ( file Path); FilterBySuffix filter = new FilterBySuffix(".deb"); String[] file s = file .list(filter); int counter = 0; for (String file name: file s){ int versionStartIndex
public static void main(String[] args) { // 目录 File dataDir = new File ("E:/资源库资源/20091111"); //存放 目录 及其 子目录 下的所有 文件 对象 List my file = new ArrayList(); //开始遍历