Java File Find find(File folder, final String name)

Here you can find the source of find(File folder, final String name)

Description

Find the File with the given name in the given folder, or its subfolders.

License

Open Source License

Parameter

Parameter Description
folder the folder in which to start searching
name the name of the file to search for

Return

the file if found; null otherwise. Also returns null if the give name is blank, the given folder is null or not a directory.

Declaration

public static File find(File folder, final String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oobium, Inc.//from w  ww .  j ava 2  s.c o 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;

public class Main {
    /**
     * Find the File with the given name in the given folder, or its  subfolders.
     * @param folder the folder in which to start searching
     * @param name the name of the file to search for
     * @return the file if found; null otherwise. Also returns null if the give name
     * is blank, the given folder is null or not a directory.
     */
    public static File find(File folder, final String name) {
        if (name != null && name.length() > 0 && folder != null && folder.isDirectory()) {
            File[] files = folder.listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return (file.isDirectory() || file.getName().equals(name));
                }
            });
            for (File file : files) {
                if (file.isDirectory()) {
                    return find(file, name);
                } else {
                    return file;
                }
            }
        }
        return null;
    }
}

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)
  5. find(File file, Pattern pattern, int limit, List found, Set visited)
  6. find(File path, Boolean recursive)
  7. find(File root)
  8. find(final File fileDir, final String fileNameRegex)
  9. find(final File root, final String name)