142

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?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user385261
  • 4,069
  • 8
  • 26
  • 24

2 Answers2

279

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
}
Jonathan Larson
  • 302
  • 2
  • 9
sarnold
  • 102,305
  • 22
  • 181
  • 238
104

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.

ajduke
  • 4,991
  • 7
  • 36
  • 56