Java Utililty Methods Folder Read

List of utility methods to do Folder Read

Description

The list of methods to do Folder Read are organized into topic(s).

Method

StringgetFileSimpleName(final File f)
Returns the simple file name (sans extension) for the passed file
if (f == null)
    throw new RuntimeException("The passed file was null", new Throwable());
final String name = f.getName();
final int index = name.indexOf('.');
if (index == -1)
    return name;
return name.substring(0, index);
ListgetFilesInClassPath(String path, String endsWith)
get Files In Class Path
Set list = new TreeSet();
StringTokenizer st = new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
while (st.hasMoreTokens()) {
    String pathElement = st.nextToken();
    File file = new File(pathElement, path);
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
...
ListgetFilesIncludeSubdirectories(final File directory)
Get all files in directory and all subdirectories.
List<File> result = new ArrayList<File>();
getFilesIncludeSubdirectories(directory, result);
return result;
File[]getFilesInFolder(File dir)
Returns all the Files in the specified directory and all sub-directories.
return files(dir).toArray(new File[0]);
ListgetFilesInFolder(final String pathToFolder, final String suffix)
get Files In Folder
File folder = new File(pathToFolder);
String[] files = folder.list(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.toUpperCase().endsWith(suffix.toUpperCase());
});
List<String> ret = new ArrayList<String>();
for (String file : files) {
...
File[]getFilesInFolderByRegex(File folder, final String regex)
get Files In Folder By Regex
if (!folder.exists()) {
    throw new FileNotFoundException();
if (!folder.isDirectory()) {
    throw new IllegalStateException(
            "execution jar is suppose to be in this folder, but the object present is not a directory: "
                    + folder);
File[] matchingFiles = folder.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
        if (name.matches(regex))
            return true;
        else
            return false;
});
if (matchingFiles == null) {
    throw new IllegalStateException(
            "the File[] matchingFiles variable is null.  This means an IOException occured while doing listFiles.  Please check disk availability and retry again");
return matchingFiles;
File[]getFilesMatchingRegexp(final File directory, final String regexp)
get Files Matching Regexp
final Pattern pattern = Pattern.compile(regexp);
return getFilesMatchingRegexp(directory, pattern);
LonggetFilesModDate(String path)
get Files Mod Date
File file = new File(path);
return file.lastModified();
File[]getFilesOf(File dir)
get Files Of
if (dir == null) {
    throw new IllegalArgumentException("argument must not be null.");
File[] files = dir.listFiles();
if (files == null) {
    throw new IllegalArgumentException("could not get files of [" + dir + "]");
return files;
...
ListgetFilesOfDirectory(File directory)
get Files Of Directory
return getFilesOfDirectory(directory, null);