-1

Possible Duplicate:
Recursively list files in Java

I think File[] files = folder.listFiles() can only list the first level of files. Is there a way to list files recursively?

Community
  • 1
  • 1
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

1 Answers1

2

Not a built-in one, but you can write a short recursive program to do walk the directory tree recursively.

void listAll(File dir, List<File> res) {
    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            listAll(f, res);
        } else {
            res.add(f);
        }
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523