Java File List Load getFileList(String dir)

Here you can find the source of getFileList(String dir)

Description

get File List

License

Apache License

Declaration

public static String[] getFileList(String dir) 

Method Source Code

//package com.java2s;
/**//from w  ww . j a va2 s .c  o m
 * Copyright (c) 2015 https://github.com/zhaohuatai
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.io.File;

import java.io.FilenameFilter;

import java.util.regex.Pattern;

public class Main {
    public static String[] getFileList(String dir) {
        try {
            File parent = new File(dir);
            if (!parent.isAbsolute() || !parent.isDirectory()) {
                return null;
            }
            return parent.list();
        } catch (Exception e) {
            return null;
        }
    }

    public static String[] getFileList(final String dir, final String pattern) {
        try {

            File parent = new File(dir);
            if (!parent.isAbsolute() || !parent.isDirectory()) {
                return null;
            }
            final Pattern namePattern = Pattern.compile(pattern);
            return parent.list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    if (namePattern.matcher(name).matches()) {
                        return true;
                    } else {
                        return false;
                    }
                }
            });

        } catch (Throwable te) {
            return null;
        }
    }
}

Related

  1. getFileList(final File dir, final String extension, final List list, final int maxDepth)
  2. getFileList(final List fileList, final File root, final File[] ignoreList)
  3. getFileList(final String location)
  4. getFileList(Map>> map, File dir, T key)
  5. getFileList(String dir)
  6. getFileList(String dir, final String extension)
  7. getFileList(String dir, final String suffix)
  8. getFileList(String directory, String filterRegexp)
  9. getFileList(String directoryPath)