import java.io.*; public class Practice_3 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File dir = new File("c:\\新建文件夹\\note"); String[] arr = dir.list(new FilenameFilter() { public boolean accept(File dir ,String name) { return true; //如果返回值为false,则默认地址中无文件, 所以list底层调用了该方法。 } } ); sop(arr.length); for( String s : arr) { sop(s); } } public static void sop(Object obj) { System.out.println(obj); } }
过滤器:
import java.io.*; public class Practice_3 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File dir = new File("c:\\新建文件夹\\note"); String[] arr = dir.list(new FilenameFilter() { public boolean accept(File dir ,String name) { sop("dir::"+dir+"....name::"+name); return name.endsWith(".java"); } } ); sop(arr.length); for( String s : arr) { sop(s); } } public static void sop(Object obj) { System.out.println(obj); } }