类似于创建新文件,文本过滤器也有两种。
new FileFilter(){};文本过滤器1,得到路径名字:c:/folder/123.txtnew FilenameFilter(){};文本过滤器2,得到文件路径和名字:c:/folder,123.txt1 package file; 2 3 import java.io.*; 4 5 public class TestDir01 { 6 public static void main(String[] args) { 7 File dir=new File("d:/folder01"); 8 MyFilter filter=new MyFilter(); 9 String[] fileName=dir.list(filter);10 for(String name:fileName){11 System.out.println(name);12 }13 // 类似于创建新文件,文本过滤器也有两种。14 // new FileFilter(){};文本过滤器1,得到路径名字:c:/folder/123.txt15 // new FilenameFilter(){};文本过滤器2,得到文件路径和名字:c:/folder,123.txt16 String[]fileNamen=dir.list(new FilenameFilter() {17 //用法一,匿名内部类18 @Override19 public boolean accept(File dir, String name) {20 return name.toLowerCase().endsWith(".txt");21 }22 });23 for(String name:fileNamen){24 System.out.println(name);25 }26 }27 // 用法二,创建内部类28 private static class MyFilter implements FilenameFilter{29 30 @Override31 public boolean accept(File dir, String name) {32 if(name.toLowerCase().endsWith(".jpg")){33 return true;34 }35 return false;36 }37 38 }39 }