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.

Share:
322,676
user385261
Author by

user385261

Updated on February 03, 2020

Comments

  • user385261
    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
    ajduke about 13 years
    do import File class in above code like this - import java.io.File;
  • Ripon Al Wasim
    Ripon Al Wasim about 11 years
    package is imported in java exactly, not class
  • ajduke
    ajduke about 11 years
    @RiponAlWasim can you elaborate more ?
  • Ripon Al Wasim
    Ripon Al Wasim about 11 years
    You 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
    ajduke about 11 years
    @RiponAlWasim ok, got it. Thanks !
  • Ahmed Laatabi
    Ahmed Laatabi over 7 years
    it does not work with me !