Java 實例 - 在指定目錄中查找文件
以下實例演示了在 C 盤中查找以字母 'b' 開頭的所有文件:
/* author by w3cschool.cc 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); } } } }
以上代碼運行輸出結(jié)果為:
build build.xml
更多建議: