Java Folder Read getFiles(File dir)

Here you can find the source of getFiles(File dir)

Description

Recursively gets all the files in a directory

License

Open Source License

Parameter

Parameter Description
dir the directory to traverse for files

Declaration

public static File[] getFiles(File dir) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/*  w  ww .j ava2  s.  co  m*/
     * Recursively gets all the files in a directory
     *
     * @param dir the directory to traverse for files
     */
    public static File[] getFiles(File dir) {
        List<File> list = new LinkedList<File>();
        getAllFilesRecursively(dir, list);
        File[] arr = new File[list.size()];
        list.toArray(arr);
        return arr;
    }

    private static void getAllFilesRecursively(File dir, List<File> filelist) {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        int i;
        for (i = 0; i < files.length; ++i) {
            if (files[i].isFile()) {
                filelist.add(files[i]);
            } else if (files[i].isDirectory()) {
                getAllFilesRecursively(files[i], filelist);
            }
        }
    }
}

Related

  1. getFiles(Collection folders)
  2. getFiles(File apkFile, Pattern regex)
  3. getFiles(File archive)
  4. getFiles(File dir)
  5. getFiles(File dir)
  6. getFiles(File dir, File... excludes)
  7. getFiles(File dir, final String pattern)
  8. getFiles(File dir, String... names)