Using a filter when listing the contents of a directory in Java

The easiest way is to use an anonymous inner class which implements either FileFilter or FilenameFilter:

//list only pdf files
File[] pdfFiles = departmentFolder.listFiles(new FilenameFilter(){
  public boolean accept(File dir, String name){
    return (name.endsWith("pdf")||name.endsWith("PDF"));
//list only folders
File[] usrFolders = departmentFolder.listFiles(new FileFilter(){
  public boolean accept(File pathname){
    return (pathname.isDirectory());