Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By pbrockway2

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

  1. #1
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    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.

    Java Code:
    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();
          }
        }
    The destination path is made up of the user's home directory and the Test name
    Java Code:
    String dtpath = new JFileChooser().getFileSystemView().getDefaultDirectory().toString();
    String destpath = dtpath + "\\Videobenchmark\\";

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

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

    DarrylBurke likes this.

  3. #3
    simminni's Avatar
    simminni is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

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

    Hi,

    First, if you are trying to creating a child folder inside parent folder that does not exist, Use mkdirs() instead mkdir().
    mkdirs() will construct all the non existing folders for you. Ex: in "C:/temp/folder1" we want to create folder1 inside temp folder that doesn't exist, in such cases we should use mkdirs()
    Sample Code:

    Java Code:
    import java.io.File;
    class MakingDirectory
    {
    public static void main(String []args)
    {
    	File dir = new File("C:\\Test\\folder1");
    	dir.mkdirs();
    }
    }

    Second, if you are using mkdir(), please make sure you are passing appropriate string for location.
    Ex: To create a "Temp"
    Invalid string: "C:\\Temp"
    Valid string: "C:\\Temp\\"

    Sample code:
    Java Code:
    import java.io.File;
    class MakingDirectory
    {
    public static void main(String []args)
    {
    	File dir = new File("C:\\Test\\");
    	System.out.println(dir.mkdir());
    }
    }
    Hope that helps. Cheers :)
    Last edited by simminni; 06-27-2013 at 11:50 AM. Reason: added code tags

  4. #4
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

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

    Yes this did work for me Simminni. Thanks a million!

  5. #5
    simminni's Avatar
    simminni is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

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

    You are welcome :)

Similar Threads

  1. Creating File inside a Directory
    By viswanadh7 in forum AWT / Swing
    Replies: 2
    Last Post: 06-27-2013, 08:18 PM
  2. Problem on mkdir
    By JavaCy in forum New To Java
    Replies: 4
    Last Post: 03-25-2011, 04:18 PM
  3. Creating a Group in Active Directory
    By Bumbsiii in forum Advanced Java
    Replies: 6
    Last Post: 01-17-2011, 09:09 AM
  4. Replies: 2
    Last Post: 05-11-2009, 11:07 AM
  5. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •