Java 实例 - 遍历指定目录下的所有目录
Java 教程
· 2019-02-11 07:59:27
以下实例演示了如何使用 File 类的 list 方法来遍历指定目录下的所有目录:
Main.java 文件
import java.io.*;
class Main {
public static void main(String[] args) {
File dir = new File("F:");
File[] files = dir.listFiles();
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
files = dir.listFiles(fileFilter);
System.out.println(files.length);
if (files.length == 0) {
System.out.println("目录不存在或它不是一个目录");
}
else {
for (int i=0; i< files.length; i++) {
File filename = files[i];
System.out.println(filename.toString());
}
}
}
}
以上代码运行输出结果为:
14 F:\C Drive Data Old HDD F:\Desktop1 F:\harsh F:\hharsh final F:\hhhh F:\mov F:\msdownld.tmp F:\New Folder F:\ravi F:\ravi3 F:\RECYCLER F:\System Volume Information F:\temp F:\work
点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html
Release It!
Michael T. Nygard / Pragmatic Bookshelf / 2007-03-30 / USD 34.95
“Feature complete” is not the same as “production ready.” Whether it’s in Java, .NET, or Ruby on Rails, getting your application ready to ship is only half the battle. Did you design your system to......一起来看看 《Release It!》 这本书的介绍吧!