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.
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.
Create a single directory.
Create a directory named “Directory2 and all its sub-directories “Sub2″ and “Sub-Sub2″ together.
Source: this perfect tutorial , you find also an example of use.
if you want to be sure its created then this:
beacuse
mkDir()
returns a boolean, and findbugs will cry for it if you dont use the variable. Also its not nice...mkDir()
returns only true ifmkDir()
creates it. If the dir exists, it returns false, so to verify the dir you created, only callexists()
ifmkDir()
return false.assertThat()
will checks the result and fails ifexists()
returns false. ofc you can use other things to handle the uncreated directory.You can try FileUtils#forceMkdir
This library have a lot of useful functions.
For java 7 and up:
This the way work for me do one single directory or more or them: need to import java.io.File;
/*enter the code below to add a diectory dir1 or check if exist dir1, if does not, so create it and same with dir2 and dir3 */