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:
* / java.io. 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:. ArrayListVsHashMap.java Found:. CombiningExceptions.java Found:. ConvertingToMicroseconds.java Found:. ConvertToOctal.java Found:. UsingDirectoryStream.java
Read: Top Java IDEs