例子
以下示例显示java.io.File.listFiles(FileFilter filter)方法的用法。
package com.jc2182;
import java.io.File;
import java.io.FileFilter;
public class FileDemo {
public static void main(String[] args) {
File f = null;
File[] paths;
try {
// create new file
f = new File("c:/test");
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
};
// returns pathnames for files and directory
paths = f.listFiles(filter);
// for each pathname in pathname array
for(File path:paths) {
// prints file and directory paths
System.out.println(path);
}
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
c:\test\child_test.txt
c:\test\child_test.xlsx