Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a directory in Java?

Tags:

java

directory

How do I create Directory/folder?

Once I have tested System.getProperty("user.home");

I have to create a directory (directory name "new folder" ) if and only if new folder does not exist.

like image 725
jimmy Avatar asked Sep 03 '10 10:09

jimmy


People also ask

How do you create a directory in Java?

In Java, the mkdir() function is used to create a new directory. This method takes the abstract pathname as a parameter and is defined in the Java File class. mkdir() returns true if the directory is created successfully; else, it returns false​.

How do you create a new directory?

Create a New Directory ( mkdir ) The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory using cd . Then, use the command mkdir followed by the name you would like to give the new directory (e.g. mkdir directory-name ).

What is a directory in Java?

A file system structure containing files and other directories is called directories in java, which are being operated by the static methods in the java. nio. file. files class along with other types of files, and a new directory is created in java using Files.


2 Answers

new File("/path/directory").mkdirs(); 

Here "directory" is the name of the directory you want to create/exist.

like image 156
Bozho Avatar answered Oct 11 '22 21:10

Bozho


After ~7 year, I will update it to better approach which is suggested by Bozho.

File theDir = new File("/path/directory"); if (!theDir.exists()){     theDir.mkdirs(); } 
like image 25
jmj Avatar answered Oct 11 '22 19:10

jmj