Java File Find find(String name, File dir)

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

Description

Find a file in an extracted OAR directory

License

Apache License

Parameter

Parameter Description
name The filename to find. If the first character of the name is '*', then the match will be the first file that ends with the remaining string
dir The parent directory to start the search from

Return

The file, or null if not found

Declaration

public static File find(String name, File dir) 

Method Source Code

//package com.java2s;
/*//from  w  w  w . j  a  v  a2s  .  c o m
 * Copyright 2008 the original author or authors.
 *
 * 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;

public class Main {
    /**
     * Find a file in an extracted OAR directory
     *
     * @param name The filename to find. If the first character of the
     * name is '*', then the match will be the first file that ends with the
     * remaining string
     * @param dir The parent directory to start the search from
     *
     * @return The file, or null if not found
     */
    public static File find(String name, File dir) {
        File found = null;
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    found = find(name, file);
                    if (found != null)
                        break;
                }
                if (name.startsWith("*")) {
                    if (file.getName().endsWith(name)) {
                        found = file;
                        break;
                    }
                } else {
                    if (file.getName().equals(name)) {
                        found = file;
                        break;
                    }
                }
            }
        }

        return found;
    }
}

Related

  1. find(File root)
  2. find(final File fileDir, final String fileNameRegex)
  3. find(final File root, final String name)
  4. find(String _sSourceString, String _sReg, int group)
  5. find(String executable, File... dirs)
  6. find(String path, String matcher)
  7. find(String path, String suffix)
  8. find2(File baseDir, FileFilter filter, List files, boolean includeHiddenFiles)
  9. findByExt(File base, String ext)