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 3 of 3

Thread: Maing a directory using mkdir()

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Maing a directory using mkdir()

    I am trying to make a file and a containing folder inside my programs source.
    the code doesnt seem to ever thing the file is created, and the folder/file never show up.

    import java.io.File;
    import java.io.FilenameFilter;
     
    public class Startup {
     
    	Print p = new Print();
    	File data = new File("data\\data.txt");
    	ReadingLine rl = new ReadingLine();
     
    	String temp="";
     
    	@SuppressWarnings("static-access")
    	public Startup()
    	{
    		if(!data.exists())
    		{
    			data.mkdir();  //this doesnt make the folder data or file data.txt
    			p.pl("Hello, I am System, Since this is your first time using me I need to ask you some questions.");
    			p.pl("What is your name?");
    			temp = rl.readLine();
    			User.setName(temp);
     
    		}
    	}
    }


  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: Maing a directory using mkdir()

    See the API for File, the mkdir() returns a boolean if and only if the directory was created - you can use this to evaluate programatically if the directory was created. Typically, if it returns false something is incorrect with either the Path or permissions. In this case, the path in the 'data' File object seems to be a the path to a file, and I presume the data directory does not exist? You must first create the directory, then create the file - and you do not create a file via the mkdir, this creates directories. To create a file, write to a new file or use the createNewFile method, and its parent directory must exist. In other words, you do not create a folder and directory in one pass (eg the parent folders are not created for you), you must do so independently through different objects.

    Edit: Thread moved from the tutorials sections. Please choose the section you post in more carefully in the future.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maing a directory using mkdir()

    Thanks, It works now.
    And sorry about posting in the wrong place.

Similar Threads

  1. How to copy files from one directory to another directory
    By kewlkeny in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 25th, 2012, 07:36 AM
  2. Re: Accessing Package from a Different Directory
    By smolina in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 7th, 2010, 03:47 PM
  3. using "user.dir" with mkdir
    By meisterluv in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 13th, 2010, 09:28 PM
  4. make dir using mkdir() in java io
    By mssi in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 6th, 2010, 04:51 PM
  5. Class directory
    By Kit in forum Java Theory & Questions
    Replies: 7
    Last Post: October 11th, 2009, 10:07 AM