使用api 实现对目录的递归遍历,现简单实现如下:
import java.io.File;
//文件目录类
public class MyDir {
private String path;
// 构造函数
public MyDir(String path) {
super();
this.path = path;
}
// 显示文件信息
public void showAll() {
File f = new File(path);
if (f.exists() && f.isDirectory()) {
System.out.println(f.getPath() + "\t<DIR>\t Start-------");
File[] files = f.listFiles(); // 返回抽象路径名数组
for (File f1 : files) {
if (f1.isDirectory()) {
MyDir dir = new MyDir(f1.getPath());
dir.showAll();
} else if (f1.isFile()) {
System.out.println(f1.getPath());
}
}
System.out.println(f.getPath() + "\t<DIR>\t End-------\n");
}
}
public static void main(String[] args) {
MyDir dir = new MyDir("C:\\WINDOWS");
dir.showAll();
}
}
import java.io.File;
//文件目录类
public class MyDir {
private String path;
// 构造函数
public MyDir(String path) {
super();
this.path = path;
}
// 显示文件信息
public void showAll() {
File f = new File(path);
if (f.exists() && f.isDirectory()) {
System.out.println(f.getPath() + "\t<DIR>\t Start-------");
File[] files = f.listFiles(); // 返回抽象路径名数组
for (File f1 : files) {
if (f1.isDirectory()) {
MyDir dir = new MyDir(f1.getPath());
dir.showAll();
} else if (f1.isFile()) {
System.out.println(f1.getPath());
}
}
System.out.println(f.getPath() + "\t<DIR>\t End-------\n");
}
}
public static void main(String[] args) {
MyDir dir = new MyDir("C:\\WINDOWS");
dir.showAll();
}
}