CoreJava/exp10/ListFiles.java

28 lines
866 B
Java
Raw Normal View History

2024-05-09 09:10:51 +08:00
package exp10;
import java.io.File;
public class ListFiles {
public static void main(String[] args) {
String dirPath = "D:/JavaFile"; //要扫描的路径
list(dirPath);
}
//递归遍历dirPath里的所有文件,并打印java文件的绝对路径
public static void list(String dirpath){
File dir = new File(dirpath);
String[] files = dir.list(); //获取该文件夹内的所有文件
for (String filename: files){
String filePath = dirpath+"/"+filename;
File f = new File(filePath);
if(f.isDirectory()){ //如果是文件夹则递归遍历
list(filePath);
}else if(f.getName().endsWith(".java")){ //如果是java文件则打印绝对路径
System.out.println(filePath);
}else{
}
}
}
}