Reputation: 503
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.
Upvotes: 22
Views: 49584
Reputation: 61
Use Files.createDirectory(Paths.get(strTempNewDirPath)); instead of mkdir().
Upvotes: 0
Reputation: 71
make sure there are no dots in the directory name. For example: "ab.c"should be changed to "abc".
Upvotes: 0
Reputation: 1
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();
Upvotes: 0
Reputation: 15552
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
Upvotes: 44
Reputation: 361
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
Upvotes: -1
Reputation: 21618
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
Upvotes: 21
Reputation: 1794
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.
Upvotes: 0