Java实现的文件过滤代码分享(按后辍过滤)

时间:2021-12-11 14:23:18

好久没有写代码了,也好久没有更新我的博客了,昨晚写了这个过滤文件名的程序,遂发之~

  1. /*name:FileNameFilter 
  2. *author : Runzhen Wang  
  3. *date:2009/11/04 
  4. */ 
  5.   
  6. import java.util.*; 
  7. import java.io.*; 
  8. import java.lang.*; 
  9.   
  10. class FileNameFilter{ 
  11.   public void filter(String strPath,String fname){ 
  12.     File f=new File(strPath); 
  13.     String s=new String(); 
  14.     if(f.isDirectory()){ 
  15.       File[] fList =f.listFiles(); 
  16.       for(int i=0;i<fList.length;i++){ 
  17.          if(fList[i].isFile()&&fList[i].getName().endsWith(fname)){ 
  18.            System.out.println(fList[i].getName()); 
  19.          } 
  20.       } 
  21.     } 
  22.   
  23.   } 
  24.   
  25. public class FileNameFilterDemo{ 
  26.   public static void main(String[] args){ 
  27.     FileNameFilter fnf=new FileNameFilter(); 
  28.     Scanner kb=new Scanner(System.in); 
  29.     String str1=new String(); 
  30.     String str2=new String(); 
  31.     System.out.print(“输入文件目录:”); 
  32.     str1=kb.next(); 
  33.     System.out.print(“输入过滤后缀名:”); 
  34.     str2=kb.next(); 
  35.     fnf.filter(str1,str2); 
  36.   }