Here you can find the source of fileExists(File directory, String fileName)
Parameter | Description |
---|---|
directory | The directory to search in. |
fileName | The file name to look for. |
public static boolean fileExists(File directory, String fileName)
//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()]); } }