28 lines
866 B
Java
28 lines
866 B
Java
|
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{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|