Reputation: 679
I tried to make it as easy as possible.
Example:
File f = new File("non_existing_dir/someDir");
System.out.println(f.mkdir());
System.out.println(f.mkdirs());
mkdir()
and mkdirs()
return both false
°_°. Both work (create directory) if i use double backslash \\
(like "\\non_existing_dir\\someDir"
BUT:
if I do .toURI()
after that I receive: file:/Users/MyName/Desktop/%5Cnon_existing_dir%5CsomeDir/
if I do .getPath()
i receive: \non_existing_dir\someDir
if I do .getCanonicalPath()
I receive: /Users/MyName/Desktop/\non_existing_dir\someDir
So i want to have instead this results:
with .toURI()
receiving: file:/Users/MyName/Desktop
/non_existing_dir/someDir/
with .getPath()
receiving: /non_existing_dir/someDir
and with .getCanonicalPath()
receiving: /Users/MyName/Desktop
/non_existing_dir/someDir
Thanks in advance to everyone.
Upvotes: 0
Views: 1148
Reputation: 26961
If non_existing_dir
does not exists, you can check getParentFile()
and create it with mkdir()
.
Also avoid problems between OS with File.separator
.
String filename = "non_existing_dir" + File.separator + "someDir";
File f = new File(filename);
if (!f.exists()) {
if (!f.getParentFile().exists()) {
// make the dir
f.getParentFile().mkdir();
}
f.mkdir();
}
Upvotes: 1