21

I am using Eclipse and jdk1.7. I am making a basic program using file handling, in which an output directory inside the directory is to be made. But when I run the program, the output is showing false and the directory is not made. I thought that the output was false because of the presence of a directory with the same name, but this is not the reason. So I need help. Here is my code:

import java.io.File;

public class P {
    public static void main(String[] args) {
        File f1 = new File ("abc");
        File f2 = new File (f1,"abc");
        System.out.println(f2.mkdir());
    }
}

Its output is false and yet no directory has been created. How can I resolve this problem? This is not only in this program - each and every program in which I am calling the method mkdir() is having the same problem.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
user2529216
  • 493
  • 1
  • 3
  • 11
  • does "abc" exist and is a directory? did you look for the new directory in the working directory of your program? (print new File("").getAbsolutePath() to find out the WD) – A4L Jul 19 '13 at 14:05
  • 1
    For diagnostics, print [`f2.getAbsolutPath()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getAbsolutePath()). Maybe its trying to create the directories in a location where you don't have write access? – mthmulders Jul 19 '13 at 14:07
  • Java 7 introduced the Files class that uses exceptions instead of boolean return values. This can be useful for getting a text description of what failed for future debugging. – Michael Krussel Jul 19 '13 at 15:17

6 Answers6

42

You have to use mkdirs() with an s if you want to create multiple directories. It is probably also worth checking that you canWrite() to the location as some places are permissioned. Both of these are on the File class

Vishrant
  • 15,456
  • 11
  • 71
  • 120
RNJ
  • 15,272
  • 18
  • 86
  • 131
20

its obj.mkdirs()

have a look to this:

File  f = new File("non_existing_dir/someDir");
System.out.println(f.mkdir());
System.out.println(f.mkdirs());

The first print won't create a directory and returns false but the second does and returns true

tokhi
  • 21,044
  • 23
  • 95
  • 105
0

Create directory example

it looks like you'll need to work on your path a bit as it doesn't look like File will infer "abc."

Also, make sure you have permissions on the path you're attempting to create the directory. If you don't, it will fail. It has been a while since I've played with Java, so not sure if you'd need to do mkdir calls the entire way down the path (ie /here/, /here/now-here/, /here/now-here/final) or not. Think it may be recursive but that'd be worth verifying.

Actually, from looking at the other answers looks like mkdirs would be recursive, mkdir is not. I'd go with mkdirs especially if the input isn't going to be known from the onset otherwise you'll end up writing a function with mkdir that does the exact same thing.

Robert
  • 1,745
  • 5
  • 34
  • 61
0

calling the only file.mkdirs() often doesn't work. call it in an evaluation such as -

if(file.mkdirs()){ //do something}

Or, in an assignment such as -

 boolean result = file.mkdirs();
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
0

make sure there are no dots in the directory name. For example: "ab.c"should be changed to "abc".

-1

mkdir needs the abstract path, not the relative path. try to use...

File f2 = new File (f1, "C:\\");

... for example.

From Java DOC:

public boolean mkdir()

Creates the directory named by this abstract pathname.

Returns:

true if and only if the directory was created; false otherwise

Throws:

SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not permit the named directory to be created

Community
  • 1
  • 1
cristi
  • 361
  • 3
  • 9