Here you can find the source of find(File folder, final String name)
Parameter | Description |
---|---|
folder | the folder in which to start searching |
name | the name of the file to search for |
public static File find(File folder, final String name)
//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; } }