Here you can find the source of getFileListing(File aStartingDir)
static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException
//package com.java2s; /**/*from w w w.j a va 2s . c o m*/ * Copyright (C) 2008 by * * Cam-Tu Nguyen * ncamtu@gmail.com * College of Technology * Vietnam National University, Hanoi * * Thu-Trang Nguyen * trangnt84@gmail.com * College of Technology * Vietnam National University, Hanoi * * JNSP is a free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, * or (at your option) any later version. * * JNSP is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with JNSP; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException { validateDirectory(aStartingDir); List<File> result = new ArrayList<File>(); File[] filesAndDirs = aStartingDir.listFiles(); List<File> filesDirs = Arrays.asList(filesAndDirs); for (File file : filesDirs) { if (!file.isFile()) { //must be a directory //recursive call! List<File> deeperList = getFileListing(file); result.addAll(deeperList); } else { if (file.getName().endsWith(".txt")) result.add(file); //add file *.txt only } } return result; } static private void validateDirectory(File aDirectory) throws FileNotFoundException { if (aDirectory == null) { throw new IllegalArgumentException("Directory should not be null."); } if (!aDirectory.exists()) { throw new FileNotFoundException("Directory does not exist: " + aDirectory); } if (!aDirectory.isDirectory()) { throw new IllegalArgumentException("Is not a directory: " + aDirectory); } if (!aDirectory.canRead()) { throw new IllegalArgumentException("Directory cannot be read: " + aDirectory); } } }