Java Files list() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, Files.list(dir) returns a Stream<Path> of the files and folders directly inside dir. It is not recursive: the contents of subfolders are not included, and the special entries . and .. never appear.

Entries come in no particular order, so sort them if order matters. The stream keeps the directory open, so close it with try-with-resources.

A static method of the final utility class java.nio.file.Files, available since Java 8. Call it on the class name, as in Files.list(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Open the stream in a try-with-resources header:

Java
try (Stream<Path> entries = Files.list(dir)) {
    entries.map(Path::getFileName).sorted().forEach(System.out::println);
}
CallWhat it does
Files.list(dir)Direct children only.
Files.walk(dir)Everything below, recursively.
Files.newDirectoryStream(dir, glob)Children matching a pattern.

Since Java 8. It is built on newDirectoryStream; I/O errors while iterating arrive as UncheckedIOException.

Library declarations
Java
public static Stream<Path> list(Path dir) throws IOException

Parameters

ParameterDescription
dirThe directory to list.

Return Value

Return ValueDescription
Stream<Path>Lazily populated entries, each resolved against dir.

Compare with Similar Methods

Listing tools:

MethodHow it differs
walk()Recursive stream of the whole tree.
newDirectoryStream()Java 7; for-each loop, glob filter.
find()Recursive search with a condition.

Java Examples

Example 1 — List a Folder Without Its Subfolders

A file inside docs is not listed, because list looks only one level deep.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListDirectChildren {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path docs = Files.createDirectory(dir.resolve("docs"));
        Files.write(dir.resolve("b.txt"), new byte[0]);
        Files.write(dir.resolve("a.txt"), new byte[0]);
        Files.write(docs.resolve("inner.txt"), new byte[0]);
        List<String> names;
        try (Stream<Path> entries = Files.list(dir)) {
            names = entries.map(p -> p.getFileName().toString()).sorted().collect(Collectors.toList());
        }
        System.out.println("Entries: " + names);
        System.out.println("inner.txt listed: " + names.contains("inner.txt"));
        Files.delete(docs.resolve("inner.txt"));
        Files.delete(docs);
        Files.delete(dir.resolve("a.txt"));
        Files.delete(dir.resolve("b.txt"));
        Files.delete(dir);
    }
}

How It Works

  1. Three children. The temp folder holds two files and the docs folder; docs/inner.txt is one level further down.
  2. Names, then sort. Each Path is reduced to its file name and sorted, because the system may return entries in any order.
  3. Not recursive. inner.txt is missing from the list; use walk to include it.

Example 2 — Separate Files from Folders

Filter with Files::isRegularFile for files, then open a second stream to count folders.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ListFilesOnly {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Files.write(dir.resolve("todo.txt"), new byte[0]);
        Files.write(dir.resolve("notes.txt"), new byte[0]);
        Files.createDirectory(dir.resolve("img"));
        Files.createDirectory(dir.resolve("music"));
        List<String> files;
        try (Stream<Path> entries = Files.list(dir)) {
            files = entries.filter(Files::isRegularFile)
                    .map(p -> p.getFileName().toString())
                    .sorted()
                    .collect(Collectors.toList());
        }
        long folders;
        try (Stream<Path> entries = Files.list(dir)) {
            folders = entries.filter(Files::isDirectory).count();
        }
        System.out.println("Files: " + files);
        System.out.println("Folders: " + folders);
        for (String name : new String[] {"todo.txt", "notes.txt", "img", "music"}) {
            Files.delete(dir.resolve(name));
        }
        Files.delete(dir);
    }
}

How It Works

  1. Method reference as filter. Files::isRegularFile keeps only ordinary files and drops img and music.
  2. One stream, one pass. A stream cannot be reused, so counting folders opens a fresh Files.list.
  3. Sorted names, plain count. notes.txt sorts before todo.txt; a count needs no sorting because it does not depend on order.

Example 3 — An Empty Folder, a File, and a Missing Path

An empty folder gives an empty stream; a file or a missing path throws when list is called.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.util.stream.Stream;

public class ListErrors {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path empty = Files.createDirectory(dir.resolve("empty"));
        Path plain = Files.write(dir.resolve("plain.txt"), new byte[0]);
        try (Stream<Path> s = Files.list(empty)) {
            System.out.println("empty: " + s.count() + " entries");
        }
        try (Stream<Path> s = Files.list(plain)) {
            System.out.println("plain.txt listed");
        } catch (NotDirectoryException e) {
            System.out.println("plain.txt: NotDirectoryException");
        }
        try (Stream<Path> s = Files.list(dir.resolve("missing"))) {
            System.out.println("missing listed");
        } catch (NoSuchFileException e) {
            System.out.println("missing: NoSuchFileException");
        }
        Files.delete(empty);
        Files.delete(plain);
        Files.delete(dir);
    }
}

How It Works

  1. Empty is fine. Listing an empty folder succeeds and the stream simply has no elements.
  2. Wrong kind of path. plain.txt is a file, so list throws NotDirectoryException before any stream exists.
  3. Nothing there. A missing path fails with NoSuchFileException; both are subclasses of FileSystemException.

Frequently Asked Questions

Use try (Stream<Path> s = Files.list(dir)) { ... } on Java 8+, or Files.newDirectoryStream(dir) on Java 7.
No. It returns only the direct children. Use Files.walk(dir) for every level.
In no particular order; it depends on the file system. Sort the stream if you need a stable order.
Yes. It holds an open directory handle until closed, so use try-with-resources.
It throws NotDirectoryException.

Next: Files.move()

Rename a file or folder, or move it to another directory, with optional replace and atomic modes.

move() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful