递归显示一个目录下面的所有文件
import java.io.*;
import java.util.*;
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2("d:\\test");
ArrayList list = t.getList();
...}
public Test2(String path) {
this.path = path;
File dir = new File(path);
getDirectory(dir);
}
public void getDirectory(File file) {
String[] fileList = file.list();
if (fileList == null) {
return;
}
for (int i = 0; i < fileList.length; i++) {
File f = new File(file, fileList[i]);
if (f.isDirectory()) {
dirList.add(fileList[i]);
getDirectory(f); //递归
}
}
}
public ArrayList getList() {
return dirList;
}
private String path;
private ArrayList dirList = new ArrayList();
}
import java.util.*;
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2("d:\\test");
ArrayList list = t.getList();
...}
public Test2(String path) {
this.path = path;
File dir = new File(path);
getDirectory(dir);
}
public void getDirectory(File file) {
String[] fileList = file.list();
if (fileList == null) {
return;
}
for (int i = 0; i < fileList.length; i++) {
File f = new File(file, fileList[i]);
if (f.isDirectory()) {
dirList.add(fileList[i]);
getDirectory(f); //递归
}
}
}
public ArrayList getList() {
return dirList;
}
private String path;
private ArrayList dirList = new ArrayList();
}