Java2024/src/chapter7/Example05.java
2024-12-10 10:39:13 +08:00

28 lines
698 B
Java

package chapter7;
import java.io.File;
public class Example05 {
public static void main(String[] args) {
File file = new File("src");
showFiles(file);
}
/**
* 显示指定目录下的所有文件
* @param dir 目录
*/
public static void showFiles(File dir) {
// 获取当前目录下所有文件的数组
File[] fileArray = dir.listFiles();
for (File file : fileArray) {
if (file.isDirectory()) {
// 如果是目录就递归调用
showFiles(file);
}
// 打印文件的绝对路径
System.out.println(file.getAbsolutePath());
}
}
}