Here you can find the source of getAllFiles(final String path, final String suffix, boolean recurse)
Parameter | Description |
---|---|
path | eg "/usr/local/metrics" |
suffix | Case insensitive: eg ".DBF" |
recurse | set to true to recurse into all the child sub directories. |
public final static List<File> getAllFiles(final String path, final String suffix, boolean recurse)
//package com.java2s; /*//from www.j ava 2s .c om * Copyright (c) 2006 Stephan D. Cote' - All rights reserved. * * This program and the accompanying materials are made available under the * terms of the MIT License which accompanies this distribution, and is * available at http://creativecommons.org/licenses/MIT/ * * Contributors: * Stephan D. Cote * - Initial API and implementation */ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Main { /** The Unix separator character. */ public static final char UNIX_SEPARATOR = '/'; /** The Windows separator character. */ public static final char WINDOWS_SEPARATOR = '\\'; /** * Scan the given directory for files containing the substrMatch * Small case extensions '.dbf' are recognized and returned as '.DBF' * * @param path eg "/usr/local/metrics" * @param suffix Case insensitive: eg ".DBF" * @param recurse set to true to recurse into all the child sub directories. * * @return a list of File objects representing the files in the given path with the given suffix */ public final static List<File> getAllFiles(final String path, final String suffix, boolean recurse) { File folder = new File(path); File[] listOfFiles = folder.listFiles(); final List<File> list = new ArrayList<File>(20); String upperSuffix = null; if (suffix != null) upperSuffix = suffix.toUpperCase(); if (listOfFiles != null) { for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { if ((upperSuffix == null) || listOfFiles[i].getName().toUpperCase().endsWith(upperSuffix)) { list.add(listOfFiles[i]); } } else if (listOfFiles[i].isDirectory()) { if (recurse) { try { list.addAll(getAllFiles(listOfFiles[i].getCanonicalPath(), suffix, recurse)); } catch (IOException e) { e.printStackTrace(); } } } } } return list; } /** * Gets the name minus the path from a full filename. * * <p>This method will handle a file in either Unix or Windows format. The * text after the last forward or backslash is returned.<pre> * a/b/c.txt --> c.txt * a.txt --> a.txt * a/b/c --> c * a/b/c/ --> "" * </pre> * * <p>The output will be the same irrespective of the machine on which the * code is running.</p> * * @param filename the filename to query, null returns null * * @return the name of the file without the path, or an empty string if none exists */ public static String getName(String filename) { if (filename == null) { return null; } int index = indexOfLastSeparator(filename); return filename.substring(index + 1); } /** * Returns the index of the last directory separator character. * <p> * This method will handle a file in either Unix or Windows format. * The position of the last forward or backslash is returned. * <p> * The output will be the same irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there * is no such character */ public static int indexOfLastSeparator(String filename) { if (filename == null) { return -1; } int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR); int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR); return Math.max(lastUnixPos, lastWindowsPos); } }