FileFilter, FilenameFilter用法和文件排序

时间:2023-03-08 20:54:39

FileFilter和FilenameFilter这两个类的用法都很简单,都只有一个方法

FileFilter
/**
* @param pathname The abstract pathname to be tested
*/
boolean accept(File pathname)
用法示例:
; i < htmlFiles.length; i++) {
      System.out.println(htmlFiles[i]);
    }
  }
}

class HTMLFileFilter implements FileFilter {

public boolean accept(File pathname) {

if (pathname.getName().endsWith(".html"))
      return true;
    if (pathname.getName().endsWith(".htm"))
      return true;
    return false;
  }
}

FilenameFilter
/**
* @param dir - the directory in which the file was found.
* @param name - the name of the file.
*/
boolean accept(File dir,  String name)
用法示例:
; i < s.length; i++) {
      System.out.println(s[i]);
    }
  }
}
文件排序
排序规则:目录排在前面,按字母顺序排序文件列表
List;
    ;
    return o1.getName().compareTo(o2.getName());
    }
});

for(File f : files)
    System.out.println(f.getName());

from: www.hilyb.com