Here you can find the source of getFilesForDirectory(File dir)
Parameter | Description |
---|---|
dir | The directory |
public static Collection<File> getFilesForDirectory(File dir)
//package com.java2s; /********************************************************************** Copyright (c) 2004 Andy Jefferson and others. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at//from w w w .j a va 2 s . c om http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Contributors: ... **********************************************************************/ import java.io.File; import java.util.Collection; import java.util.HashSet; public class Main { /** * Method to return the files below the specified directory. * @param dir The directory * @return The files */ public static Collection<File> getFilesForDirectory(File dir) { if (dir == null) { return null; } Collection files = new HashSet(); File[] dirFiles = dir.listFiles(); if (dirFiles != null) { for (int i = 0; i < dirFiles.length; i++) { if (dirFiles[i].isFile()) { files.add(dirFiles[i]); } else { // Check for files in subdirectories Collection childFiles = getFilesForDirectory(dirFiles[i]); if (childFiles != null && childFiles.size() > 0) { files.addAll(childFiles); } } } } return files; } }