

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a directory in Java
A directory can be created with the required abstract path name using the method java.io.File.mkdir(). This method requires no parameters and it returns true on the success of the directory creation or false otherwise.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("c:\\demo1\\"); file.createNewFile(); boolean flag = file.mkdir(); System.out.print("Directory created? " + flag); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
Directory created? false
Note − The output may vary on Online Compilers.
Now let us understand the above program.
The method java.io.File.mkdir() is used to create a directory in Java. Then the boolean value returned by this method is printed. A code snippet that demonstrates this is given as follows −
try { File file = new File("c:\\demo1\\"); file.createNewFile(); boolean flag = file.mkdir(); System.out.print("Directory created? " + flag); } catch(Exception e) { e.printStackTrace(); }
- Related Questions & Answers
- How to create a directory using Java?
- How to create a Directory recursively using Java?
- How to create a directory hierarchy using Java?
- How to create a directory in project folder using Java?
- Create temporary file in specified directory in Java
- Delete a directory in Java
- How to create a new directory by using File object in Java?
- How to create a directory using Python?
- How to create a Directory using C#?
- How to create a directory recursively using Python?
- How to create directory programmatically in Android?
- How to create a symbolic link to a directory in Ubuntu?
- How to create a unique directory name using Python?
- Check whether a file is a directory in Java
- Get Java Home Directory
Advertisements