imain 发表于 2013-2-5 02:40:00

FilenameFilter接口 示例

import java.io.*;
public class OnlyExt implements FilenameFilter{
 String strExt;
 public OnlyExt(String strExt){
  this.strExt = "." + strExt;
 }
 public boolean accept(File fleDir,String strName){
  return strName.endsWith(strExt);
 }
}
//Directory of .TXT files.
import java.io.*;
class DirListOnly{
 public static void main(String[] args)
 {
  String strDirName = "c:/winnt";
  File f1 = new File(strDirName);
  FilenameFilter only = new OnlyExt("txt");
  String s[] = f1.list(only);
  for(int i=0;i<s.length;i++){
   System.out.println(s); 
  }
 }
}
页: [1]
查看完整版本: FilenameFilter接口 示例