Here you can find the source of isFileExistByRegex(String dir, final String regex)
Parameter | Description |
---|---|
dir | given dir |
regex | regulation expression |
public static boolean isFileExistByRegex(String dir, final String regex)
//package com.java2s; import java.io.File; import java.io.FilenameFilter; public class Main { /**/*from w ww.ja v a 2s.c o m*/ * Check if has the files match the regulation expression under the given * dir. * * @param dir * given dir * @param regex * regulation expression * @return true the file exists; false the file not exists. */ public static boolean isFileExistByRegex(String dir, final String regex) { if (regex == null) { return false; } File file = new File(dir); if (!file.isDirectory()) { return file.exists(); } String[] str = getFileListByRegex(dir, regex); if (str == null || str.length < 1) { return false; } return true; } public static boolean isDirectory(String path) { File file = new File(path); return file.isDirectory(); } /** * Get the File list under the given directory by the regex. If the * parameter dir is not a directory, return null. * * @param dir * the given directory * @param regex * the given regulate expression. * @return */ public static String[] getFileListByRegex(String dir, final String regex) { File file = new File(dir); if (!file.isDirectory()) { return null; } FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return name.matches(regex); } }; return file.list(filter); } }