Java File Exist fileExists(File directory, String fileName)

Here you can find the source of fileExists(File directory, String fileName)

Description

Checks if a file with specified name exists in the directory.

License

Open Source License

Parameter

Parameter Description
directory The directory to search in.
fileName The file name to look for.

Declaration

public static boolean fileExists(File directory, String fileName) 

Method Source Code


//package com.java2s;

import java.io.File;

import java.util.LinkedList;

public class Main {
    /**//from w ww. ja  v a 2s .  c o  m
     * Checks if a file with specified name exists in the directory.
     * 
     * @param directory
     *            The directory to search in.
     * @param fileName
     *            The file name to look for.
     * @return
     */
    public static boolean fileExists(File directory, String fileName) {
        return new File(directory, fileName).exists();
    }

    /**
     * Checks if within directory exists one or more files with prefix
     * specified.
     * 
     * @param directory
     *            The directory to search in.
     * @param prefix
     *            Prefix of files to be searched for.
     * @return
     */
    public static boolean fileExists(File directory, String prefix, String suffix) {
        final String f_prefix = prefix != null ? prefix : "";
        final String f_suffix = suffix != null ? suffix : "";

        for (File f : directory.listFiles()) {
            if (f.getName().startsWith(f_prefix) && f.getName().endsWith(f_suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Lists all files starting with prefix specified located in the directory.
     * 
     * @param directory
     *            The directory to search in.
     * @param prefix
     *            Prefix of files to be listed.
     * @param suffix
     *            Suffix of files to be listed.
     * @return
     */
    public static File[] listFiles(File directory, String prefix, String suffix) {
        final String f_prefix = prefix != null ? prefix : "";
        final String f_suffix = suffix != null ? suffix : "";

        LinkedList<File> files = new LinkedList<>();
        for (File f : directory.listFiles()) {
            if (f.getName().startsWith(f_prefix) && f.getName().endsWith(f_suffix)) {
                files.add(f);
            }
        }

        return files.toArray(new File[files.size()]);
    }
}

Related

  1. fileExist(String _FileName, String _Path)
  2. fileExist(String cmdPath)
  3. fileExist(String p_filename)
  4. fileExist(String path)
  5. fileExists(File dir, String regex)
  6. fileExists(File file)
  7. fileExists(File path)
  8. fileExists(final String path)
  9. fileExists(final String path)