Java File Find findJavaFiles(File folder)

Here you can find the source of findJavaFiles(File folder)

Description

find Java Files

License

Open Source License

Declaration

public static File[] findJavaFiles(File folder) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oobium, Inc.//ww w  .  ja v  a  2  s  . co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Jeremy Dowdall <jeremy@oobium.com> - initial API and implementation
 ******************************************************************************/

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static File[] findJavaFiles(File folder) {
        return findFiles(folder, true, ".java");
    }

    /**
     * Find all files in the given folder and its subfolders.
     * Note this only returns visible files - directories and hidden files are not included.
     * @param folder
     * @return all files in the given folder and its subfolders.
     */
    public static File[] findFiles(File folder) {
        return findFiles(folder, true, new String[0]);
    }

    private static File[] findFiles(File folder, boolean accept$, String... endsWith) {
        if (folder != null && folder.isDirectory()) {
            List<File> list = new ArrayList<File>();
            addFiles(list, folder, accept$, endsWith);
            return list.toArray(new File[list.size()]);
        } else {
            return new File[0];
        }
    }

    public static File[] findFiles(File folder, String... endsWith) {
        return findFiles(folder, true, endsWith);
    }

    private static void addFiles(List<File> list, File folder, final boolean accept$, final String... endsWith) {
        File[] files = folder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return !file.isHidden()
                        && (file.isDirectory() || endsWith.length == 0 || endsWith(file.getName(), endsWith))
                        && (accept$ || !file.getName().contains("$"));
            }
        });
        for (File file : files) {
            if (file.isDirectory()) {
                addFiles(list, file, accept$, endsWith);
            } else {
                list.add(file);
            }
        }
    }

    private static void addFiles(List<File> list, File folder, final String name) {
        File[] files = folder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return (file.isDirectory() || name == null || file.getName().equals(name));
            }
        });
        for (File file : files) {
            if (file.isDirectory()) {
                addFiles(list, file, name);
            } else {
                list.add(file);
            }
        }
    }

    private static boolean endsWith(String name, String[] endsWith) {
        for (String end : endsWith) {
            if (name.endsWith(end)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. findExecutableLocation(String executableName)
  2. findExecutableOnPath(String executable)
  3. findExecutableOnPath(String executableName)
  4. findExecutableOnPath(String executableName)
  5. findJavaFiles(File dir, Collection filenames)