Since Java 7, you can use the file visitor pattern to visit the contents of a directory recursively.
The documentation for the FileVisitor interface is here.
This allows you to iterate over files without creating a large array of File objects.
Simple example to print out your file names:
Path start = Paths.get(new URI("file:///my/folder/")); Files.walkFileTree(start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { System.out.println(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } } });