🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the File list() method in Java programming and how to use it with an example.
1. File list() Method Overview
Definition:
The list() method of the File class returns an array of strings naming the files and directories in the directory denoted by the File object. If the File object denotes a file or doesn't exist, it will return null.
Syntax:
public String[] list()
Parameters:
None.
Key Points:
- This method is useful for obtaining the names of all files and directories inside a specified directory.
- If the method is called on a File object that represents a file (not a directory) or if the directory doesn't exist, it will return null.
- If the directory is empty, it returns an empty array.
- The method does not provide information about sub-directories beyond their names.
- The result does not include special entries like "." and "..".
- The order of names returned is not specified and can vary over time.
2. File list() Method Example
import java.io.File;
public class FileListExample {
public static void main(String[] args) {
// Creating a File object for a directory
File directory = new File("path/to/directory");
// Getting the list of files and directories
String[] contents = directory.list();
// Checking if contents is null or not
if (contents != null) {
for (String name : contents) {
System.out.println(name);
}
} else {
System.out.println("The provided path does not denote a directory or the directory doesn't exist.");
}
}
}
Output:
file1.txt subDirectory1 file2.jpg ...
Explanation:
In the demonstrated example, a File object, directory, is created representing a specific directory.
Invoking the list() method on this object fetches the names of all files and directories within the specified directory. These names are then printed out to the console. If the specified path was not a directory or if it didn't exist, the method would return null and the output would state that the provided path doesn't denote a directory.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment