How to create empty folder in java?
322,676
Solution 1
Looks file you use the .mkdirs()
method on a File
object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
Solution 2
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.
Author by
user385261
Updated on February 03, 2020Comments
-
user385261 about 4 years
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile". However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
-
ajduke about 13 yearsdo import File class in above code like this - import java.io.File;
-
Ripon Al Wasim about 11 yearspackage is imported in java exactly, not class
-
ajduke about 11 years@RiponAlWasim can you elaborate more ?
-
Ripon Al Wasim about 11 yearsYou can write as import java.io.*; or, mentioning specific class as import java.io.File; Anyway, import statement is not the main focus of the question
-
ajduke about 11 years@RiponAlWasim ok, got it. Thanks !
-
Ahmed Laatabi over 7 yearsit does not work with me !