以下实例演示了使用 File 类的 dir.list() 方法在指定目录中查找所有文件列表:
- /*
- author by shouce.ren
- Main.java
- */
- import java.io.File;
- public class Main {
- public static void main(String[] argv)
- throws Exception {
- File dir = new File("../java");
- String[] children = dir.list();
- if (children == null) {
- System.out.println("该目录不存在");
- }
- else {
- for (int i = 0; i < children.length; i++) {
- String filename = children[i];
- System.out.println(filename);
- }
- }
- }
- }
以上代码运行输出结果为:
- Car.class
- FileUtil.class
- FileUtil.java
- HelloWorld.class
- HelloWorld.java
- HelloWorldDebug.class
- HelloWorldDebug.java
- ……