You could do it like that:

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
if(listOfFiles != null) {
 for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
 }
}

Do you want to only get JPEG files or all files?

Answer from RoflcoptrException on Stack Overflow
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › listing all files in a directory in java
Listing All Files in a Directory in Java
October 1, 2022 - List fileNamesList = new ArrayList(); Files.newDirectoryStream(Paths.get(dir), path -> path.toString().endsWith(".java")).forEach(filePath -> fileNamesList.add(filePath.toString())); Reply · I appreciate the post.
🌐
Baeldung
baeldung.com › home › java › java io › list files in a directory in java
List Files in a Directory in Java | Baeldung
January 8, 2024 - First, we used listFiles() to get all the contents of the folder. Then we used DirectoryStream to lazy load the directory’s content. We also used the list() method introduced with Java 8.
🌐
Java Concept Of The Day
javaconceptoftheday.com › home › list all files in directory – with java 8 examples
List All Files In Directory - With Java 8 Examples
February 5, 2019 - They are – Files.walk() and Files.list(). The main difference between these two methods is, Files.walk() recursively traverses the given directory and it’s sub-directories to get list of all the files and folders in the hierarchy.
🌐
Niraj Sonawane
nirajsonawane.github.io › 2018 › 05 › 23 › java-8-List-All-Files-in-Directory
Java 8 List All Files in Directory | Niraj Sonawane
May 24, 2018 - List All Files in DirectoryFiles.list Method Return a lazily populated Stream, the elements of which are the entries in the directory. We Can use the stream operations to find Specific Files, List fil
🌐
Niraj Sonawane
nirajsonawane.github.io › 2018 › 05 › 29 › Java-8-List-all-Files-in-Directory-and-Subdirectories
Java 8 List all Files in Directory and Subdirectories | Niraj Sonawane
June 10, 2018 - Files.list Method Return a lazily populated Stream for the current directory only,Files.walk can be used to get list of files from Directory & Subdirectories .
🌐
Stack Abuse
stackabuse.com › java-list-files-in-a-directory
Java: List Files in a Directory
September 12, 2019 - In Java 8 and later we can use the java.nio.file.Files class to populate a Stream and use that to go through files and directories, and at the same time recursively walk all subdirectories. Note that in this example we will be using Lambda Expressions: public class FilesWalk { public static ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-list-all-files-in-a-directory-in-java
How to List all Files in a Directory in Java? - GeeksforGeeks
April 28, 2025 - We have used the listFiles() method of the File class to retrieve an array of File objects representing the files and directories contained within the specified directory. We iterate over the array of File objects and print the name of each ...
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › java – how to list all files in a directory?
Java - How to list all files in a directory? - Mkyong.com
December 25, 2018 - package com.mkyong.example; import java.io.File; import java.util.ArrayList; import java.util.List; public class JavaExample { public static void main(String[] args) { final File folder = new File("C:\\projects"); List<String> result = new ArrayList<>(); search(".*\\.java", folder, result); for (String s : result) { System.out.println(s); } } public static void search(final String pattern, final File folder, List<String> result) { for (final File f : folder.listFiles()) { if (f.isDirectory()) { search(pattern, f, result); } if (f.isFile()) { if (f.getName().matches(pattern)) { result.add(f.getAbsolutePath()); } } } } } ... Founder of Mkyong.com, passionate Java and open-source technologies.
🌐
Java Development Journal
javadevjournal.com › home › list all files from a directory in java
List all files from a directory in Java | Java Development Journal
June 14, 2017 - Learn how to List all files from a directory in Java recursively using Java8 Steam, NIO or Apache Commons IO.The best method for recursive iteration?
🌐
YouTube
youtube.com › watch
How to List all files in a folder using Java 8 - YouTube
How to List all files in a folder using Java 8 Java8 Lambda
Published   November 20, 2024
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › create-files-in-directory
Java: How to list the files in a directory | alvinalexander.com
I use the Apache FileUtils class for a lot of needs like this, but here’s a quick example of a Java class that show how to create a list of all files in a directory using just the Java File class. Specifically, this example shows how to list all the files in a directory, store those filenames in a String array, and then print the array. Here’s the source code for this “list files in a directory” Java class:
🌐
Javatpoint
javatpoint.com › list-all-files-in-a-directory-in-java
List All Files in a Directory in Java - Javatpoint
List All Files in a Directory in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-list-all-files-in-a-directory-using-Java.php
How to List All Files in a Directory Using Java
We this set this array of files equal to all the files in the Documents directory. This is done by invoking the listFiles() function on the dir object. Remember the dir object is set equal to the Documents directory. We then use a foor loop in Java. This is called an enhanced for loop.
🌐
Netjstech
netjstech.com › 2017 › 04 › reading-all-files-in-folder-java-program.html
Read or List All Files in a Folder in Java | Tech Tutorials
February 28, 2023 - Test abc.txt Test1 test.txt test1.txt Test2 xyz.txt · There are two ways to list all the files in a folder; one is using the listFiles() method of the File class which is there in Java from 1.2. Another way to list all the files in a folder is to use Files.walk() method which is a recent addition ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-get-list-of-all-files-folders-from-a-folder-in-java
How to display all the files in a directory using Java
September 12, 2023 - The following is an another example to display all the files in a directory. import java.io.File; public class ReadFiles { public static File folder = new File("C:\\Apache24\\htdocs"); static String temp = ""; public static void main(String[] args) { System.out.println("Reading files under the folder "+ folder.getAbsolutePath()); listFilesForFolder(folder); } public static void listFilesForFolder(final File folder) { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { if (fileEntry.isFile()) { temp = fileEntry.getName(); if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("txt"))System.out.println( "File = " + folder.getAbsolutePath()+ "\\" + fileEntry.getName()); } } } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-listfiles-method-in-java-with-examples
File listFiles() method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate the // use of listFiles() function import java.io.*; public class solution { public static void main(String args[]) { // try-catch block to handle exceptions try { // Create a file object File f = new ...