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
๐ŸŒ
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.
๐ŸŒ
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 - ... // Java program list all files ... an object for specific directory File directory = new File(directoryPath); // Using listFiles method we get all the files of a directory // return type of listFiles is array File[] files = ...
๐ŸŒ
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 - Collect all filtered entries into a List. //The source directory String directory = "C:/temp"; // Reading only files in the directory try { List<File> files = Files.list(Paths.get(directory)) .map(Path::toFile) .filter(File::isFile) .collect(Collectors.toList()); files.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } DirectoryStream is part of Java 7 and is used to iterate over the entries in a directory in for-each loop style.
๐ŸŒ
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 โ€บ 29 โ€บ Java-8-List-all-Files-in-Directory-and-Subdirectories
Java 8 List all Files in Directory and Subdirectories | Niraj Sonawane
June 10, 2018 - List All Files in Directory and SubdirectoriesFiles.walk Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. Files.list Method Return a lazily
๐ŸŒ
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()); } } } } } The above code sample will produce the following result.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-list-all-files-in-a-directory
How to list all files in a directory in Java
August 21, 2022 - The Files.list() static method from NIO API provides the simplest way to list the names of all files and folders in a given directory: try (Stream<Path> paths = Files.list(Paths.get("~/java-runner"))) { // print all files and folders ...
Find elsewhere
๐ŸŒ
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 void main(String[] args) { try (Stream<Path> walk = Files.walk(Paths.get("D:/Programming"))) { // We want to find only regular files List<String> result = walk.filter(Files::isRegularFile) .map(x -> x.toString()).collect(Collectors.toList()); result.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } }
๐ŸŒ
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 - try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) { List<String> result = walk.map(x -> x.toString()) .filter(f -> f.endsWith(".java")).collect(Collectors.toList()); result.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-list-all-files-in-a-directory-and-nested-sub-directories
Java Program to List all Files in a Directory and Nested Sub-Directories - GeeksforGeeks
July 23, 2025 - For main directory files, we use foreach loop. ... // Recursive Java program to print all files // in a folder(and sub-folders) import java.io.File; public class GFG { static void RecursivePrint(File[] arr, int level) { // for-each loop for main directory files for (File f : arr) { // tabs for internal levels for (int i = 0; i < level; i++) System.out.print("\t"); if (f.isFile()) System.out.println(f.getName()); else if (f.isDirectory()) { System.out.println("[" + f.getName() + "]"); // recursion for sub-directories RecursivePrint(f.listFiles(), level + 1); } } } // Driver Method public static
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ blog โ€บ post โ€บ java โ€บ create-files-in-directory
Java: How to list the files in a directory | alvinalexander.com
import java.io.File; import java.util.Arrays; public class DirectoryTestMain { public static void main(String[] args) { // create a file that is really a directory File aDirectory = new File("C:/temp"); // get a listing of all files in the directory String[] filesInDir = aDirectory.list(); // sort the list of files (optional) // Arrays.sort(filesInDir); // have everything i need, just print it now for ( int i=0; i<filesInDir.length; i++ ) { System.out.println( "file: " + filesInDir[i] ); } } }
๐ŸŒ
CodeJava
codejava.net โ€บ java-se โ€บ file-io โ€บ list-files-and-directories-recursively-in-a-directory
Java File IO - List files and directories recursively
July 29, 2019 - import java.io.File; /** * * @author www.codejava.net * */ public class ListDirectoryRecurisve { public void listDirectory(String dirPath, int level) { File dir = new File(dirPath); File[] firstLevelFiles = dir.listFiles(); if (firstLevelFiles != null && firstLevelFiles.length > 0) { for (File aFile : firstLevelFiles) { for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + aFile.getName() + "]"); listDirectory(aFile.getAbsolutePath(), level + 1); } else { System.out.println(aFile.getName()); } } } } public static void main(String[] args
๐ŸŒ
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
With Java, we can get all files of any directory on a computer. In Java, there is a listFiles() functions, which allows us to get all files in any directory that we want.
๐ŸŒ
Dev.java
dev.java โ€บ learn โ€บ java-io โ€บ file-system โ€บ listing
Listing the Content of a Directory - Dev.java
January 25, 2023 - You can list all the contents of a directory by using the newDirectoryStream(Path) method. This method returns an object that implements the DirectoryStream interface. The DirectoryStream interface extends Iterable, so you can iterate through ...
๐ŸŒ
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
๐ŸŒ
Java Guides
javaguides.net โ€บ 2019 โ€บ 07 โ€บ get-all-files-from-current-directory-in-java.html
List Files in Directory in Java
June 21, 2024 - In this example, the listFiles() method is used to list all files and directories within the specified directory. The java.nio.file.Files class, introduced in Java 7, provides more powerful and flexible methods for file operations.
๐ŸŒ
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 - import java.io.BufferedReader; ... File("G:\\Test"); ListFiles listFiles = new ListFiles(); System.out.println("reading files before Java8 - Using listFiles() method"); listFiles.listAllFiles(folder); System.out.println("-------------------------------------------------"); ...