54

I am looking for the solution to list all the files from root/Android device.

Suppose there are three folders inside the root directory, but I want to display all the files in all of these folders in a single list...

Now if am using

File f = new File("/sdcard");

Then it will list all the files from the sdcard folder only... And if I will use

File f = new File("/download");

Then it will list all the files from download folder only ..and if I will use

File f = new File("/");

Then it will list only the root directory files...not the files inside /sdcard or /download.

So what steps shall I follow to list all the files with a filter to list only .csv files from all the folder inside root?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kanika
  • 10,648
  • 18
  • 61
  • 81

3 Answers3

135

Try this:

 .....
 List<File> files = getListFiles(new File("YOUR ROOT"));
 ....
 private List<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<File>();
    File[] files = parentDir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            inFiles.addAll(getListFiles(file));
        } else {
            if(file.getName().endsWith(".csv")) {
                inFiles.add(file);
            }
        }
    }
    return inFiles;
}

Or a variant without recursion:

private List<File> getListFiles2(File parentDir) {
    List<File> inFiles = new ArrayList<>();
    Queue<File> files = new LinkedList<>();
    files.addAll(Arrays.asList(parentDir.listFiles()));
    while (!files.isEmpty()) {
        File file = files.remove();
        if (file.isDirectory()) {
            files.addAll(Arrays.asList(file.listFiles()));
        } else if (file.getName().endsWith(".csv")) {
            inFiles.add(file);
        }
    }
    return inFiles;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
  • 2
    Hi Vyacheslav Shilkin, your code worked for me but output is coming after a delay of few seconds(2-3 seconds). Can you suggest something to make it bit quicker? – abhishek kumar gupta May 24 '13 at 07:31
  • Great answer, +1 for posting a solution that doesn't use recursion. – the-ginger-geek Aug 07 '17 at 05:15
  • What are the properties of them? Potentially extreme memory pressure in one of the two? What about performance? For example, include some benchmarks (incl. information about the platform (hardware, versions of Android, etc.), SD card type/performance category). – Peter Mortensen Dec 29 '22 at 21:47
3

I've modified Vyacheslav's solution because I needed only file names inside the directory.

...
List<String> files = getList(new File("YOUR ROOT"));
...
private List<String> getList(File parentDir, String pathToParentDir) {

    ArrayList<String> inFiles = new ArrayList<String>();
    String[] fileNames = parentDir.list();

    for (String fileName : fileNames) {
        if (fileName.toLowerCase().endsWith(".txt") || fileName.toLowerCase().endsWith(".rtf") || fileName.toLowerCase().endsWith(".txd")) {
            inFiles.add(pathToParentDir + fileName);
        } else {
            File file = new File(parentDir.getPath() + "/" + fileName);
            if (file.isDirectory()) {
                inFiles.addAll(getList(file, pathToParentDir + fileName + "/"));
            }
        }
    }

    return inFiles;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Micer
  • 8,731
  • 3
  • 79
  • 73
  • 2
    Sorry to say, but your modification are no improvements. First of all you add directories with .txt example and I really do not know what pathToParentDir is supposed to be that parentDir.getParent() cannot fix. Lastly, using "/" is discouraged, use File.separator instead. – Michel Apr 19 '14 at 06:17
2

You can use the following method,

private int readLogList(String filePath)
{
    File directory = Environment.getExternalStorageDirectory();

    File folder = new File(directory + ConstantCodes.FILE_SEPARATOR + filePath);

    if (!folder.exists())
    {
        return 0;
    }

    return folder.list().length;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lucifer
  • 29,392
  • 25
  • 90
  • 143