Java Folder Read getFilesByDate(String directory, final boolean earliestFirst)

Here you can find the source of getFilesByDate(String directory, final boolean earliestFirst)

Description

get Files By Date

License

Open Source License

Declaration

public static File[] getFilesByDate(String directory, final boolean earliestFirst) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import java.util.List;

public class Main {
    public static File[] getFilesByDate(String directory, final boolean earliestFirst) {
        File[] files = new File(directory).listFiles();

        Arrays.sort(files, new Comparator<File>() {

            @Override//  w  w w. jav  a2 s. c o m
            public int compare(File o1, File o2) {
                if (earliestFirst) {
                    return new Long(o1.lastModified()).compareTo(new Long(o2.lastModified()));
                } else {
                    return new Long(o2.lastModified()).compareTo(new Long(o1.lastModified()));
                }
            }
        });

        return files;
    }

    public static String[] listFiles(String fileName) {
        return listFiles(new File(fileName));
    }

    public static String[] listFiles(File file) {
        List<String> files = new ArrayList<String>();

        File[] fileArray = file.listFiles();
        if (fileArray != null) {
            for (int i = 0; i < fileArray.length; i++) {
                if (fileArray[i].isFile()) {
                    files.add(fileArray[i].getName());
                }
            }
        }

        return (String[]) files.toArray(new String[0]);
    }
}

Related

  1. getFiles(String rootDirectory)
  2. getFiles(String thePaths[])
  3. getFilesAndFilesSubDirectories(String directoryName, String regexPattern)
  4. getFilesArray(File baseStorePath, final String fileNameSearchPattern)
  5. getFilesAsArrayList(String path)
  6. getFilesByName(File path, String name)
  7. getFilesByPath(String toDir)
  8. getFilesByRegxFileName(String dir, final String regxName)
  9. getFilesByType(String fileType, String path)