文件基础操作

This commit is contained in:
2024-12-10 10:39:13 +08:00
parent 1de9754569
commit fdaf7334c8
4 changed files with 71 additions and 0 deletions

View File

@@ -1,4 +1,27 @@
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());
}
}
}