Recursive output specify all .java files in the directory absolute path

analysis:

A:封装目录
B:获取该目录下的所有文件或者文件的File数组
C:遍历该File数组,得到每一个File对象
D:判断该File对象是否是文件夹
	是:回到B
	否:继续判断是否以.java结尾
		是:就输出该文件的绝对路径
		否:不用管

public class test {
	
public static void main(String[] args) throws Exception {
	File f=new File("D:\\java文件");
	
	getAllPath(f);
}

private static void getAllPath(File f) throws Exception{
	// TODO Auto-generated method stub
	//获取该目录下的所有文件或文件夹
	File[] ff=f.listFiles();
	for(File x:ff){
		if(x.isDirectory()){
			getAllPath(x);
		}else{
			if(x.getName().endsWith(".java")){
				System.out.println(x.getAbsolutePath());
			}
		}
	}
}
}

He published 188 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/Ting1king/article/details/104973561
Recommended