Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java mkdir -p equivalent [duplicate]

Tags:

java

mkdir

Possible Duplicate:
Recursively create directory

What's the java-esque way to create a director(ies), and don't complain if it exist?

Quoting the man for mkdir:

-p    Create intermediate directories as required... with this option 
      specified, no error will be reported if a directory given as an 
      operand already exists.
like image 441
Adam Matan Avatar asked Apr 23 '12 12:04

Adam Matan


People also ask

What is difference between mkdir and Mkdirs?

makedirs() creates all the intermediate directories if they don't exist (just like mkdir -p in bash). mkdir() can create a single sub-directory, and will throw an exception if intermediate directories that don't exist are specified. Either can be used to create a single 'leaf' directory (dirA):

Why does mkdir return false in Java?

It returns false if the directory already exists or if there was an error creating the directory.

Does directory exist Java?

File. exists() is used to check whether a file or a directory exists or not. This method returns true if the file or directory specified by the abstract path name exists and false if it does not exist.

How do you check if a file exists in a directory using Java?

To test to see if a file or directory exists, use the exists method of the Java File class, as shown in this example: File tmpDir = new File("/var/tmp"); boolean exists = tmpDir. exists(); The existing method of the Java File class returns true if the file or directory exists, and false otherwise.


2 Answers

In Java, both, files and directories are represented as File objects.

So you can do:

File file = new File("C:/a");
file.mkdirs();

Hope that helps.

like image 65
TechSpellBound Avatar answered Oct 14 '22 19:10

TechSpellBound


You're looking for File.mkdirs().

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

like image 36
GaryF Avatar answered Oct 14 '22 20:10

GaryF