Skip to Content
Functional Programming in Java
book

Functional Programming in Java

by Venkat Subramaniam
February 2014
Intermediate to advanced
160 pages
4h 59m
English
Pragmatic Bookshelf
Content preview from Functional Programming in Java

Listing All Files in a Directory

It’s pretty simple to use the File class’s list method to list all filenames in a directory. To get all the files instead of just their names, we can use the listFiles method. That’s easy, but the challenge is how to proceed once we get the list. Rather than the long-winded traditional external iterator, we can use the elegant functional-style facility to iterate through the list. To achieve this, we have to reach out to the JDK’s new CloseableStream interface, along with some related higher-order convenience functions.

Here’s the code to list the names of all the files in the current directory.

compare/fpij/ListFiles.java
 
Files.list(Paths.get(​"."​))
 
.forEach(​System​.out::println);

To list files in a different ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Programming in Java

Functional Programming in Java

Pierre-Yves Saumont

Publisher Resources

ISBN: 9781941222690Errata Page