making directories

L

lowenbrau

Hi,

I have encountered something interesting. In order to create a directory within another directory, the parent directory must be created first. For example:

File f = new File ("parent directory/child directory");
f.mkdir();

DOES NOT WORK

File f = new File ("parent directory");
f1.mkdir();
f = new File ("parent directory/child directory");
f.mkdir();

THIS WORKS

QUESTION:

is there a way to do it without explicitly creating the parent directory first?


Cheers
ST
 
P

Pushkaraj

Hi,

I have encountered something interesting. In order to create a directory within another directory, the parent directory must be created first. For example:

File f = new File ("parent directory/child directory");
f.mkdir();

DOES NOT WORK

File f = new File ("parent directory");
f1.mkdir();
f = new File ("parent directory/child directory");
f.mkdir();

THIS WORKS

QUESTION:

is there a way to do it without explicitly creating the parent directory first?

Cheers
ST

If you scan File api neatly you could see a function
boolean mkdirs() : Creates the directory named by this abstract
pathname, including any necessary but nonexistent parent directories.
use this.
 
T

Thomas Fritsch

lowenbrau said:
I have encountered something interesting. In order to create a directory
within another directory, the parent directory must be created first.
For example:

File f = new File ("parent directory/child directory");
f.mkdir();

DOES NOT WORK

File f = new File ("parent directory");
f1.mkdir();
f = new File ("parent directory/child directory");
f.mkdir();

THIS WORKS

QUESTION:

is there a way to do it without explicitly creating the parent directory
first?
As always the Java API docs are a good for such questions.
Look up <http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html>
Carefully read about mkdir() and mkdirs().
 
A

Andrew Thompson

lowenbrau wrote:
...
I have encountered something interesting.

Interesting to people who code by typing random
statements into an editor, rather than those who
consult the documentation first, perhaps..
...In order to create a directory within another directory, the parent directory must be created first.
Un-huh..

is there a way to do it without explicitly creating the parent directory first?

1st you might try RTFM.
<http://java.sun.com/javase/6/docs/api/java/io/File.html#mkdirs()>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200708/1
 
I

Ian Wilson

lowenbrau said:
Hi,

I have encountered something interesting. In order to create a directory
within another directory, the parent directory must be created first.
For example:

File f = new File ("parent directory/child directory");
f.mkdir();

DOES NOT WORK

Have you read the documentation for File.mkdir() and File.mkdirs()?

http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#mkdir()
File f = new File ("parent directory");
f1.mkdir();
f = new File ("parent directory/child directory");
f.mkdir();

THIS WORKS

QUESTION:

is there a way to do it without explicitly creating the parent directory
first?

Yes. File.mkdirs()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top