Java File Find find(File absolutePath, FileFilter filter)

Here you can find the source of find(File absolutePath, FileFilter filter)

Description

find files which has specified suffix.

License

Apache License

Parameter

Parameter Description
absolutePath a parameter
filter a parameter

Return

list of files which has specified suffix

Declaration

public static List<File> find(File absolutePath, FileFilter filter) 

Method Source Code

//package com.java2s;
/*/*  w  w w .  jav a  2  s.  c om*/
 * 
   FileUtil.java
    
   Copyright 2004-2007 KUBO Hiroya (hiroya@cuc.ac.jp).
    
   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
    
   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.
 */

import java.io.File;
import java.io.FileFilter;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * find files which has specified suffix.
     * 
     * @param absolutePath
     * @param suffix
     * @return list of files which has specified suffix
     */
    public static List<File> find(String absolutePath, final String suffix) {
        return find(new File(absolutePath), suffix);
    }

    /**
     * find files which has specified suffix.
     * 
     * @param absolutePath
     * @param suffix
     * @return list of files which has specified suffix
     */
    public static List<File> find(File absolutePath, final String suffix) {
        return find(absolutePath, new FileFilter() {
            public boolean accept(File file) {
                return file.getName().toLowerCase().endsWith(suffix);
            }
        });
    }

    /**
     * find files which has specified suffix.
     * 
     * @param absolutePath
     * @param filter
     * @return list of files which has specified suffix
     */
    public static List<File> find(File absolutePath, FileFilter filter) {
        List<File> sourceFileList = new LinkedList<File>();
        return addFoundFileToList(sourceFileList, absolutePath, filter);
    }

    private static List<File> addFoundFileToList(List<File> resultFileList, File currentPath, FileFilter filter) {
        File[] members = currentPath.listFiles(filter);
        if (members == null) {
            return new LinkedList<File>();
        }
        Arrays.sort(members);
        for (int i = 0; i < members.length; i++) {
            File file = members[i];
            resultFileList.add(file);
        }

        members = currentPath.listFiles();
        Arrays.sort(members);
        for (int i = 0; i < members.length; i++) {
            File file = members[i];
            if (file.isDirectory()) {
                addFoundFileToList(resultFileList, file, filter);
            }
        }
        return resultFileList;
    }
}

Related

  1. find(File baseFile, String regex)
  2. find(File dir, String baseName)
  3. find(File dir, String suffix, Set ignore)
  4. find(File file)