How to Create a New Folder in Java31 Mar 2025 | 3 min read In Java, we can use the File object to create a new folder or directory. The File class of Java provide a way through which we can make or create a directory or folder. We use the mkdir() method of the File class to create a new folder. For creating a directory, we first have to create an instance of the File class and pass a parameter to that instance. This parameter is the path of the directory where we need to create it. After that, we have to invoke the mkdir() method using that file object. ![]() Let's use the mkdir() method to create a directory or folder through a Java program. CreateFolder.java Output: ![]() If we go to that location, we will see the created folder as: ![]() Note: If we enter an unavailable path, the mkdir() method will not create a folder and pass the control flow to the else part.![]() Creating Hierarchy of new FoldersThe drawback of the mkdir() method is resolved by the mkdirs() method. The mkdirs() method is more powerful than mkdir() method. The mkdirs() method creates a hierarchy of new folders or directories. It creates a folder in the same way as the mkdir() method, but it also creates the parent folders too that do not exist. Let's take an example to understand how the mkdirs() method is different from the mkdir() method. CreateFolderHierarchy.java Output: ![]() When we access the desired location, we see the created folder. If the user enters an unavailable location, the mkdirs() make it available by creating all the parent folders that do not exist in the system. ![]() |
Structure of Java Program
Java is an object-oriented programming, platform-independent, and secure programming language that makes it popular. Using the Java programming language, we can develop a wide variety of applications. To make Java handy So, before diving in depth, it is necessary to understand the basic structure of Java program in detail. Let's see which elements...
6 min read
Nim Game Java
The Nim Game is a classic mathematical game for two players, rooted in combinatorial game theory. It is played by two players, who take turns removing objects from distinct piles. The game ends when all objects are removed. The player who takes the last object...
14 min read
Java Cron Expression
The technology is constantly changing day by day. Sometimes, we are required to execute a job periodically on the server. Running the job on the server manually is a difficult task so, it cannot be done multiple times by the user or administrator. In order to...
8 min read
Array Slicing in Java
Array slicing works mainly in programming languages like Python and JavaScript, allowing developers to effortlessly remove specific array segments. However, due to the design of the language in Java, the concept of array slicing is not so simple. Despite the built-in slicing syntax, Java offers...
5 min read
Java Program to Determine Whether a Given String of Parentheses (Single Type) is Properly Nested
Correctly nesting brackets is a common problem in computer science, particularly with mathematical equations, interpreters, and compilers. If the sequence of the appropriate opening and closing parenthesis is preserved, a set of parentheses is considered "properly nested." Problem Statement Given a string containing only the characters ( and...
7 min read
Overflow and Underflow in Java
In Java, overflow can be defined as a situation when the result generated of an arithmetic operation that is performed on a numeric data type surpasses the range of that specific data type. Various numeric data types are supported by Java and they are of integer(...
9 min read
Java Developer Roles and Responsibilities
At present, Java is widely used by many tech giants. The tech giants are regularly hiring Java developers to fulfill market demand. In exchange for manpower, companies offer a high salary package for that and providing seductive offers. In this section, we will discuss the roles...
6 min read
Why main() method is always static in Java
? In Java, the main() method plays a vital role in program execution. The main() method is the first method that encounters first during execution. So, it is an entry point of a program. We cannot modify the syntax of the main() method. The only thing which we can change is the...
6 min read
Reverse A String and Reverse Every Alternative String in Java
String manipulation is a common task in programming, and Java provides various built-in methods and techniques to perform such operations efficiently. In this section, we will explore how to reverse a string and reverse every alternative substring within it using Java. Reversing a String: To reverse a string...
5 min read
Permutation and Combination in Java
In mathematics, Permutation and Combination are two important concepts. Permutation is the different arrangements of the set elements. The arrangements can be made by taking one element at a time, some element at a time and all elements at a time. Combination is the different selections...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India





