·列出目录下的信息:public String[ ] list();
·列出所有的信息以File类对象包装:public File [ ] listFiles();
范例1:列出信息
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
File file = new File("e:" + File.separator);
if (file.isDirectory()) {
String result[] = file.list();
for (int x = 0; x < result.length; x++) {
System.out.println(result[x]);
}
}
}
}
============分割线============
范例2:列出全部File类对象
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
File file = new File("e:" + File.separator);
if (file.isDirectory()) {
File result[] = file.listFiles();
for (int x = 0; x < result.length; x++) {
System.out.println(result[x]);
}
}
}
}
============分割线============
范例3:列出指定目录下的子路径
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
File file = new File("e:" + File.separator);
print(file);
}
public static void print(File file) {
if (file.isDirectory()) {
File[] result = file.listFiles();
if (result != null) {
for (int x = 0; x < result.length; x++) {
print(result[x]);
}
}
}
System.out.println(file);
}
}
