Java 實例 - 在指定目錄中查找檔
以下實例演示了在 C 盤中查找以字母 'b' 開頭的所有檔:
Main.java 檔
import java.io.*;
class Main {
public static void main(String[] args) {
File dir = new File("C:");
FilenameFilter filter = new FilenameFilter() {
public boolean accept
(File dir, String name) {
return name.startsWith("b");
}
};
String[] children = dir.list(filter);
if (children == null) {
System.out.println("目錄不存在或它不是一個目錄");
}
else {
for (int i=0; i < children.length; i++) {
String filename = children[i];
System.out.println(filename);
}
}
}
}
以上代碼運行輸出結果為:
build build.xml