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.
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):
It returns false if the directory already exists or if there was an error creating the directory.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With