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,
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
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
make sure there are no dots in the directory name. For example: "ab.c"should be changed to "abc".
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();
In your case you can make use of makedirectories method in File
class.
File dir = new File("path name");
boolean isCreated = dir.mkdirs();
Here makedirectories method will create all directories that are missing in the path which the file object represent.
Source and reference is below (explained in detail).
http://www.flowerbrackets.com/create-directory-java-program/
https://docs.oracle.com/javase/6/docs/api/java/io/File.html#canWrite%28%29
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