Java tutorial
//package com.java2s; /* * Copyright 2008-2013, ETH Zrich, Samuel Welten, Michael Kuhn, Tobias Langner, * Sandro Affentranger, Lukas Bossard, Michael Grob, Rahul Jain, * Dominic Langenegger, Sonia Mayor Alonso, Roger Odermatt, Tobias Schlueter, * Yannick Stucki, Sebastian Wendland, Samuel Zehnder, Samuel Zihlmann, * Samuel Zweifel * * This file is part of Jukefox. * * Jukefox is 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 3 of the License, or any later version. Jukefox 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 * Jukefox. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.List; public class Main { public static void addFilesRecursive(List<String> files, File location, FilenameFilter filter) { if (!location.exists()) { return; } if (!location.isDirectory()) { if (filter.accept(location.getParentFile(), location.getName())) { files.add(location.getAbsolutePath()); } } // we are in a directory => add all files matching filter and then // recursively add all files in subdirectories File[] tmp = location.listFiles(filter); if (tmp != null) { for (File file : tmp) { files.add(file.getAbsolutePath()); } } File[] dirs = location.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); if (dirs == null) { return; } for (File dir : dirs) { addFilesRecursive(files, dir, filter); } } }