Best way to list files in Java, sorted by Date Modified?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sorting files by modification time is a common requirement for log viewers, backup tools, and import jobs. In modern Java, the cleanest solution is to use the java.nio.file API, read file metadata once, and then sort the results with an explicit comparator.
Prefer Path and Files Over File
The old java.io.File API still works, but java.nio.file.Path and Files are usually a better fit. They expose richer metadata, work well with streams, and make error handling more explicit.
If you only need a quick list of entries, Files.list is often enough. The important detail is that the returned stream must be closed, so use try with resources.
That example lists files, but it does not sort them. The naive next step is to sort with a comparator that calls Files.getLastModifiedTime repeatedly. That works for small directories, but it performs a file attribute lookup every time the comparator runs, which becomes wasteful as the list grows.
Read Attributes Once, Then Sort
A better pattern is to collect each path together with its last modified timestamp, then sort the collected values. This keeps the comparator simple and avoids repeating filesystem work.
This sorts from oldest to newest. If you want the newest files first, reverse the time comparator:
The secondary comparison by file name matters more than people expect. If two files have the same timestamp, the sort stays deterministic instead of appearing random across runs.
Choosing the Right Listing Method
Files.list works for one directory level. If you need recursive traversal, use Files.walk. If you are only dealing with a very large directory and want lower overhead, DirectoryStream can also be a good option.
For many applications, the practical approach is:
- use
Files.listfor one directory - filter out directories unless you explicitly want them
- read timestamps once
- sort in memory
If your goal is just "give me the newest file", you can still follow the same pattern, but stop at the maximum entry:
That makes the intent clearer than manually tracking a running maximum in a loop.
Common Pitfalls
The most common mistake is using File.listFiles() and assuming the returned order means anything. It does not; the filesystem can return entries in any order.
Another frequent issue is calling Files.getLastModifiedTime inside the comparator. That makes sorting slower and harder to debug because I/O happens during comparison instead of during collection.
It is also easy to forget that Files.list returns a stream backed by operating system resources. If you do not close it, especially in long-running programs, you can leak directory handles.
Finally, decide whether directories should be included. A mixed list of files and folders is valid, but many tasks really want regular files only. Filter explicitly so the behavior matches the requirement.
Summary
- Prefer
java.nio.file.PathandFilesfor new Java code. - Read
lastModifiedmetadata once before sorting. - Add a secondary sort key such as file name for deterministic output.
- Use
Files.listfor one level andFiles.walkfor recursive traversal. - Close directory streams with
trywith resources to avoid leaking handles.

