如何用 Java 查找文件?
以下示例演示了通过创建文件筛选器在目录中查找特定文件的方法。以下示例显示了所有以“b”开头的文件名。
示例
import java.io.*; public 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("Either dir does not exist or is not a directory"); } else { for (int i=0; i< children.length; i++) { String filename = children[i]; System.out.println(filename); } } } }
输出
build build.xml
广告