Using DirectoryStream in Java

This Java programming tutorial shows developers how to quickly use the DirectoryStream interface to get a list of filenames of a particular type (or types) specified during the newDirectoryStream.

Below is a code example showing how to use the DirectoryStream interface to build a list of filenames of a specific type in Java:

* / import.*; import java.util. *; import java.nio.file. *; public class UsingDirectoryStream {public static void main (String args[]) {UsingDirectoryStream usingDirectoryStream = new UsingDirectoryStream (); usingDirectoryStream.proceed (); } continue private void () {String fileTypes = “*. {java}”; Path currDir = Paths.get (“.”); List pathResult = new ArrayList <> (); try (DirectoryStream directoryStream = Files.newDirectoryStream (currDir, fileTypes)) {for (path filePath: directoryStream) {pathResult.add (filePath); System.out.println (“Found:” + filePath); }} catch (DirectoryIteratorException directoryIteratorException) {System.out.println (“DirectoryIteratorException:” + directoryIteratorException); } catch (IOException ioException) {System.out.println (“IOException:” + ioException); }}} / *

Running this Java code snippet in your integrated development environment (IDE) or code editor would produce the following expected output:

[[email protected]]# java UsingDirectoryStream Found:. Found:. Found:. Found:. Found:.

Read: Top Java IDEs

Related posts

How to Use Lists in Your Java GUI Application


Top collaboration tools for software developers


Data conversion in Go


Leave a Comment