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 - As we can see, listFiles() returns an array of File objects that are the contents of the directory.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ file-listfiles-method-in-java-with-examples
File listFiles() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Exception: This method throws Security Exception if the function is not allowed read access to the file Below programs will illustrate the use of the listFiles() function Example 1: We will try to find all the files and directories in a given ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ io โ€บ File.html
File (Java Platform SE 8 )
April 21, 2026 - Javaโ„ข Platform Standard Ed. 8 ... An abstract representation of file and directory pathnames.
๐ŸŒ
Dev.java
dev.java โ€บ learn โ€บ java-io โ€บ file-system โ€บ listing
Listing the Content of a Directory - Dev.java
January 25, 2023 - The Path objects returned by the iterator are the names of the entries resolved against the directory. So, if you are listing the contents of the /tmp directory, the entries are returned with the form /tmp/a, /tmp/b, and so on. This method returns the entire contents of a directory: files, links, subdirectories, and hidden files.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ nio โ€บ file โ€บ Files.html
Files (Java Platform SE 8 )
April 21, 2026 - If the file is recognized then the content type is returned. If the file is not recognized by any of the installed file type detectors then a system-default file type detector is invoked to guess the content type. A given invocation of the Java virtual machine maintains a system-wide list of file ...
Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ list-all-files-from-a-directory-recursively-in-java
List all Files from a Directory Recursively in Java - GeeksforGeeks
April 28, 2025 - Extract the paths and add them to your list. For directories, repeat step 2 recursively. Use the directory path as the new starting point for exploring its subdirectories and their files. Java program to List Files from a Directory using the Files class to list all files recursively:
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ how would i use the java listfiles() method to get all the files in my src folder?
r/AskProgramming on Reddit: How would I use the Java listFiles() method to get all the files in my src folder?
December 5, 2018 -

My project requires the user to enter a number of files to be loaded into a search object but I was worried the user would not know the files that are allowed to be uploaded. I have several in my src folder to use and I've figured out how to use the path of the files on my wn computer, but when I zip this project up and submit it obviously my professor is not going to have the src folder in the same location. How do I specify this location in relation to the project itself? My code right now looks like this:

File[] files = new File("C:\\Users\\User\\eclipse-workspace\\test").listFiles();

And this is exactly what I want it to do I just don't know how I can get this to work everytime since the project Im working on will be saved elsewhere. I'm kind of a novice please go easy on me

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_list.asp
Java List
close() delimiter() findInLine() ... ... The List interface is part of the Java Collections Framework and represents an ordered collection of elements....
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ io โ€บ file_list.htm
Java - File list() method
The Java File list() method returns the array of files and directories in the directory defined by this abstract path name. The method returns null, if the abstract pathname does not denote a directory.
๐ŸŒ
Narkive
linuxsa.linuxsa.org.narkive.com โ€บ TKwtCKYq โ€บ java-listing-files-and-directories-in-one-step
Java - listing files and directories in one step
Post by Andrew Reid I'm struggling a bit today... See, using the File class, I can get a list of the contents of a directory and work out if it's a directory or not by creating a new File object for each entry and using the isDirectory() method to determine it's nature.
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ list-files-directory-match-filename-extension-pattern
Java: How to list all files in a directory that match a filename extension | alvinalexander.com
As you can see, this method takes the name of a directory as input, and then uses the FileUtils.listFiles method, and the WildcardFileFilter class to create a list of all files in the directory that have the same filename extension, in this case, they all end with the extension .cfg.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ listing files recursively with java
Listing Files Recursively With Java | Baeldung
August 18, 2024 - The File class in Javaโ€™s java.io package offers a straightforward, if somewhat verbose approach, to list directories recursively.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-list-files-in-a-directory
Java: List Files in a Directory
September 12, 2019 - For the sake of simplicity, all ... of files and folders in a given directory, without traversing the sub-directories, is the helper method .list(), which returns an array of Strings....
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ how-to-list-directory-contents-move-copy-delete-files
How to List, Move, Copy, and Delete Files (Java Tutorial)
November 29, 2024 - This method has a significant advantage over proprietary implementations with FileInputStream and FileOutputStream, as they were necessary before Java 7 and the NIO.2 File API: Files.copy() delegates the call to operating system-specific โ€“ and thus optimized โ€“ implementations.
๐ŸŒ
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(); }
๐ŸŒ
Marco Behler
marcobehler.com โ€บ guides โ€บ java-files
How To Work With Files In Java
December 9, 2020 - Instead, it returns a DirectoryStream, ... API in Java 8. It does, however, allow you to specify a glob pattern (like *.txt), which does the job for simple listings, and is maybe a bit easier to read than fumbling with real Streams and the corresponding filter methods. Also note, that the streams returned by both methods must also be closed (e.g. with a try-with-resources statement), otherwise the JVM will keep the file handle on ...
๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ programming-9 โ€บ [java]-can't-list-files-from-directory-909501
[SOLVED] [Java] Can't list files from directory
October 22, 2011 - Hey I am trying to list all files from directory, in to a ArrayList. My mate do not want to use list, so I made this method: Code: public ArrayList