Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Creating a new directory using new File().mkdir()

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating a new directory using new File().mkdir()

    I am trying to simply create a directory, but my code just will not accomplish it. It doesn't give me any errors but it doesn't create the directory neither. I thought that new File(path).mkdir(); would have done it for me, but it is not. Any suggestions.

    public void timedStart()
        {
          SimpleDateFormat ft = new SimpleDateFormat("M/dd/yy   hh:mm:ss a"); //3 spaces between  
          testName = testNambx.getText();
          time = timebx.getText();
          tStartTime = time;
     
     
          String path = destpath + testName;
          new File(path).mkdir();
     
          try
          {
              sdate = ft.parse(time);
     
              Timer timer = new Timer();
              timer.schedule(new CoFile(), sdate);
          }
          catch(Exception e)
          {
             e.printStackTrace();
          }
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a new directory using new File().mkdir()

    Make sure that a) The parent directory exists b) the characters in the file name are permitted by your OS c) a file/folder by that name does not already exist (I presume this not to be the case in this instance, but should check and take the appropriate action). Your code should check the returned value from mkdir to determine success/failure and act appropriately.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Creating a new directory using new File().mkdir()

    And (d) that you have sufficient rights to create the directory.

    As well as checking what mkdir() returns you could also check what "new File(path)" actually creates to make sure the directory you attempting to create is the one you intend to create. The File class offers ways of seeing the full path+name named by a File instance.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new directory using new File().mkdir()

    Well my destpath is made up of the user's home directory and the Test name.
    String dtpath = new JFileChooser().getFileSystemView().getDefaultDirectory().toString();
    String destpath = dtpath + "\\Videobenchmark\\";
    The path doesn't exist, which is why I need to create it. When the application runs, the path will be determined by the Test name that the user inputs. The user will always have permissions to their home directory.

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Creating a new directory using new File().mkdir()

    Now at j-f.org Creating a new directory using new File().mkdir().

    Please, if you start a discussion at multiple places place a link at each to the others. That way everyone taking part knows what else is being said.

    ---

    You have said where you intended the directory to be created, but did you check (with a debugger or System.out.println()) that the File instance you are using names precisely that location? Likewise did you check the return value of mkdir(): what was it?

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new directory using new File().mkdir()

    What I did check is that the string that I supplied to the new File().mkdir() is indeed the directory I needed to create. I don't quite understand checking the return value of mkdir().

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Creating a new directory using new File().mkdir()

    Assign the valuex the mkdir returns to some variable, then print that value. I don't know what copeg had in mind (if amything - it may just be that since mkdir() returns a value it wouls be good to know what it is) but it might be that mkdir() returns a value indicating "sucess" in which case you would have to rethink the assumption that the code didn't create the directory it was askec to.

    I don't think it's enough to check the string. I've been bitten too often by my own assumptions... Your code says "new File(path)". Actually print the full path and name of that File instance to double (triple, whatever) check that it really does refer to the user's home directory.

    ---

    In the event that everything does look good, post a SSCCE: code that we can all run and see the non-creation of the directory for ourselves. (Programmers solving problems are such skeptics they would make Descartes look like a simple pious Catholic. That attidude works.) Strip out anything that is incidental and post runnable code to illustrate the problem.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new directory using new File().mkdir()

    It refers to the home directory. I did a JOptionPane.showMessageDialog(null, path) and it indeed show me the correct path. It just don't create the path. new File(path).mkdir() actually returns false because the directory is not there. Knowing that the path I want is not there, I want to create it. I have also tried the an if statement, but I can't figure out the method I need to use to actually create it if it is not there.

  9. #9
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Creating a new directory using new File().mkdir()

    Answered in your crosspost!

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a new directory using new File().mkdir()

    Yes this was answered in my crosspost. I needed to use mkdirs() instead of mkdir() to actually create the missing directories. Thanks for the help.

Similar Threads

  1. Replies: 1
    Last Post: March 3rd, 2013, 01:32 PM
  2. Maing a directory using mkdir()
    By gatt427 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2012, 02:33 PM
  3. No such file or directory exist (help!)
    By hawkeye10 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 14th, 2012, 09:36 PM
  4. Checkin if a file exist within a directory
    By tash876 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 23rd, 2011, 02:16 PM
  5. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 03:39 AM