Java File Exist isFileExistByRegex(String dir, final String regex)

Here you can find the source of isFileExistByRegex(String dir, final String regex)

Description

Check if has the files match the regulation expression under the given dir.

License

Open Source License

Parameter

Parameter Description
dir given dir
regex regulation expression

Return

true the file exists; false the file not exists.

Declaration

public static boolean isFileExistByRegex(String dir, final String regex) 

Method Source Code


//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);
    }
}

Related

  1. isFileExist(String fileNameAndPath)
  2. isFileExist(String filePath)
  3. isFileExist(String filePath)
  4. isFileExist(String path)
  5. isFileExist(String sFileName)
  6. isFileExisted(String filePathname)
  7. isFileExistent(String path)
  8. isFileExistNotCreate(String fileNameAndPath)
  9. isFileExists(String f, String ext)