making directories

Discussion in 'Java' started by lowenbrau, Aug 13, 2007.

  1. lowenbrau

    lowenbrau Guest

    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
    lowenbrau, Aug 13, 2007
    #1
    1. Advertising

  2. lowenbrau

    Pushkaraj Guest

    On Aug 13, 1:31 pm, "lowenbrau" <> wrote:
    > 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.
    Pushkaraj, Aug 13, 2007
    #2
    1. Advertising

  3. Thomas Kellerer, Aug 13, 2007
    #3
  4. lowenbrau schrieb:
    > 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().

    --
    Thomas
    Thomas Fritsch, Aug 13, 2007
    #4
  5. 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
    Andrew Thompson, Aug 13, 2007
    #5
  6. lowenbrau

    Ian Wilson Guest

    lowenbrau wrote:
    >
    > 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()
    Ian Wilson, Aug 13, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    483
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    514
    Jeffry van de Vuurst
    Jul 30, 2003
  3. =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=

    Multiple bin-directories with virtual directories?

    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    814
    =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?=
    Nov 9, 2004
  4. Paul F
    Replies:
    4
    Views:
    623
    Mark Fitzpatrick
    Jun 25, 2008
  5. Adam Petrie
    Replies:
    8
    Views:
    294
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page