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 OverflowBaeldung
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.
Videos
05:57
Java Files Tutorial - 01 - List files and folders in Directory ...
How to List all files in a folder using Java 8 - YouTube
29:28
Java IO | List All Files in a Directory via java.io.File | Java ...
Java Tutorial: Print list of files & directories.
How to List Files and Folders Using Files Class in Java | File ...
Top answer 1 of 3
838
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?
2 of 3
162
Create a File object, passing the directory path to the constructor. Use the listFiles() to retrieve an array of File objects for each file in the directory, and then call the getName() method to get the filename.
List<String> results = new ArrayList<String>();
File[] files = new File("/path/to/the/directory").listFiles();
//If this pathname does not denote a directory, then listFiles() returns null.
for (File file : files) {
if (file.isFile()) {
results.add(file.getName());
}
}
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.
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.
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
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("-------------------------------------------------"); ...