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

ListgetFiles(String dir)
get Files
List<String> files = new ArrayList<>();
File folder = new File(dir);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
        files.add(listOfFiles[i].getAbsolutePath());
    } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
...
File[]getFiles(String dir)
Returns the files in the given directory (only normal files, no subdirectories).
ArrayList<File> files = new ArrayList<File>();
File[] filesOrDirs = new File(dir).listFiles();
for (File fileOrDir : filesOrDirs)
    if (fileOrDir.isFile())
        files.add(fileOrDir);
return files.toArray(new File[files.size()]);
String[]getFiles(String directoryPath)
Return list of files in given directory
File dir = new File(directoryPath);
if (dir.exists()) {
    return dir.list();
return null;
ListgetFiles(String dirName, int number)
get Files
File dir = new File(dirName);
File[] files = dir.listFiles();
List<File> selectedFiles = new ArrayList<File>();
Random random = new Random();
int i = 0;
while (i < number) {
    int randomIndex = random.nextInt(files.length);
    File randomFile = files[randomIndex];
...
CollectiongetFiles(String folderName, String prefix)
get Files
File folder = new File(folderName);
File[] files = folder.listFiles();
ArrayList<File> result = new ArrayList<File>(files.length);
for (File file : files) {
    if (file.isFile() && file.getName().startsWith(prefix)) {
        result.add(file);
return result;
ListgetFiles(String parent)
get Files
File parentFile = new File(parent);
File[] files = parentFile.listFiles();
List<File> sonFiles = new ArrayList<File>();
if (parentFile.exists()) {
    for (File temp : files) {
        if (temp.isFile()) {
            sonFiles.add(temp);
return sonFiles;
File[]getFiles(String path)
get Files
File file = new File(path);
if (file.exists()) {
    return file.listFiles();
return null;
File[]getFiles(String path)
get Files
File file = new File(path);
File[] array = file.listFiles();
Arrays.sort(array, new Comparator<File>() {
    @Override
    public int compare(File file, File newFile) {
        if (file.lastModified() < newFile.lastModified()) {
            return 1;
        } else if (file.lastModified() == newFile.lastModified()) {
...
ListgetFiles(String path)
get Files
File pathFile = new File(path);
File[] files = pathFile.listFiles();
List<File> fileList = new ArrayList<File>();
if (files != null) {
    fileList.addAll(Arrays.asList(files));
return fileList;
ListgetFiles(String path)
Returns a single file object if the argument is a path to a specific file, or a list of directory files, if the argument is a path to a folder.
File file = new File(path);
List<File> list = new ArrayList<File>();
if (file.exists()) {
    if (file.isFile()) {
        list.add(file);
    } else if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
...