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?

0

2 Answers 2

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
}
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.

5
  • 3
    do import File class in above code like this - import java.io.File;
    – ajduke
    Jan 26, 2011 at 6:55
  • 1
    package is imported in java exactly, not class Feb 27, 2013 at 6:19
  • @RiponAlWasim can you elaborate more ?
    – ajduke
    Feb 28, 2013 at 5:31
  • 1
    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 Feb 28, 2013 at 6:00
  • it does not work with me ! Nov 8, 2016 at 13:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.