列出C盘根目录下符合规则的所有文件

时间:2021-11-12 12:24:16

import java.io.*;
import java.util.regex.*;

public class DirList
 {
  public static void main(String[] args)
   {
    // TODO 自动生成方法存根
    File path = new File("c:");
    String[] list;
    if (args.length == 0)
     list = path.list();
    else
     list = path.list(new DirFilters(args[0]));
    for (int i=0;i<list.length;i++)
     System.out.println(list[i]);
   }
 }
class DirFilters implements FilenameFilter
{
 private Pattern pattern;
 public DirFilters(String regex)
  {
   pattern = Pattern.compile(regex);
  }
 public boolean accept(File dir, String name)
  {
   return pattern.matcher(new File(name).getName()).matches();
   //return true;
  }