🎓 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 mkdir() method in Java programming and how to use it with an example.
1. File mkdir() Method Overview
Definition:
The mkdir() method of the File class is used to create a directory denoted by the abstract pathname of the File object. If the parent directories do not exist, the directory will not be created.
Syntax:
public boolean mkdir()
Parameters:
None.
Key Points:
- The method will create only the directory denoted by the File object's abstract pathname.
- It will return true if and only if the directory was successfully created; false will be returned otherwise.
- If the parent directories do not exist, the directory will not be created.
- No exception is thrown if the directory couldn't be created. Instead, a boolean value is returned to indicate success or failure.
- If a file or directory already exists with the specified name, the directory will not be created.
2. File mkdir() Method Example
import java.io.File;
public class FileMkdirExample {
public static void main(String[] args) {
// Creating a File object for a directory
File directory = new File("path/to/directory/newDirectory");
// Creating the directory
boolean isCreated = directory.mkdir();
if (isCreated) {
System.out.println("Directory was successfully created.");
} else {
System.out.println("Failed to create the directory. It might already exist or its parent directory might not exist.");
}
}
}
Output:
Directory was successfully created. OR Failed to create the directory. It might already exist or its parent directory might not exist.
Explanation:
In the provided example, a File object, directory, is created representing a specific directory.
Invoking the mkdir() method on this object attempts to create the directory denoted by the File object's abstract pathname. If the directory is successfully created, a success message is printed out to the console. If not, a failure message is shown, indicating potential reasons for the failure such as the directory already existing or its parent directory not existing.
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