Results 1 to 9 of 9
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Solved: Does anyone know why I am getting the error?

    Hello,

    so I am creating the following code to create a new file. Does anyone know why it's throwing the exception?

    Java Code:
    import java.util.Scanner;
    import java.lang.Integer;
    import java.io.*;
    import java.lang.*;
    import java.io.File;
    
    class CreateNumberFile{
    	public static void main(String args[]){
    	//Ask user for filename and highest number
    	Scanner in = new Scanner(System.in);
    	System.out.println("Enter the filename");
    	String fileName = in.next();
    	System.out.println("Enter the highest number for this file");
    	int maxNumber = in.nextInt();
    	
    	System.out.print("A file titled " + fileName+ " will be created containing a");
    	System.out.println(" string of numbers with numbers ranging from 0 to: " + maxNumber);
    
    		
            // Create a File object 
            File myFile = new File("home/Users/Joe/Dropbox/Programming/Java/Projects/g2.txt");
            // Tests whether the file or directory denoted by this abstract 
            //pathname exists.
            if (myFile.exists()) {
                System.out.println(myFile.getAbsolutePath() +
                        " File already exists");
    
            } else {
    		  try{
                //creates a new, empty file named by this pathname
                myFile.createNewFile();
                System.out.println(myFile.getAbsolutePath() + 
                        " File Created Successfully");
            }
    		  catch(IOException e){
    		  	System.err.println ("Caught IOException "+e.getMessage());
      }
      }
    
    	}//main
    	}//class
    When I run it, I get the following error:

    Java Code:
    Caught IOException The system cannot find the path specified
    I thought the code would check if the file exists and if it doesn't, I would use the constructor to create the file? any ideas?
    Last edited by bigsonny; 11-26-2012 at 04:01 AM. Reason: To let others know that it's solved and perhaps to remind others not to make my mistake.

  2. #2
    rougeking is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Does anyone know why I am getting the error?

    Assuming you are using linux/ubuntu/mac.......
    the path should start with "/"...
    i.e., /home/users/.....

    and next time you ask a question...make sure entire code is there....
    i see some lines with missing ends.
    hope its useful :)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Does anyone know why I am getting the error?

    Quote Originally Posted by rougeking View Post
    Assuming you are using linux/ubuntu/mac.......
    the path should start with "/"...
    i.e., /home/users/.....

    and next time you ask a question...make sure entire code is there....
    i see some lines with missing ends.
    hope its useful :)
    Wrong, and wrong. An absolute path would start with a / and a relative path wouldn't.

    And could you point out which lines you consider to have 'missing ends'?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Does anyone know why I am getting the error?

    Quote Originally Posted by bigsonny View Post
    I thought the code would check if the file exists and if it doesn't, I would use the constructor to create the file?
    What constructor?

    Note that createNewFile() does not create directories, it can only create a File in an already existing directory.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Does anyone know why I am getting the error?

    Quote Originally Posted by DarrylBurke View Post
    What constructor?

    Note that createNewFile() does not create directories, it can only create a File in an already existing directory.

    db
    Thanks DB.

    What I calling a constructor is this line

    File myFile = new File("home/Users/Joe/Dropbox/Programming/Java/Projects/g2.txt");

    Also, there is no missing end. This is my entire code. Every line is terminated with a ";".

    DB, the directory does exist, that's where the Java file is.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Does anyone know why I am getting the error?

    So I went back and modified the code by adding a "//" to the beginning of the path and now it compiles but when I look in that directory and folder (Windows 7 by the way), I still don't see a file.

    Java Code:
    import java.util.Scanner;
    import java.lang.Integer;
    import java.io.*;
    import java.lang.*;
    import java.io.File;
    
    class CreateNumberFile{
    	public static void main(String args[]){
    	//Ask user for filename and highest number
    	Scanner in = new Scanner(System.in);
    	System.out.println("Enter the filename");
    	String fileName = in.next();
    	System.out.println("Enter the highest number for this file");
    	int maxNumber = in.nextInt();
    	
    	System.out.print("A file titled " + fileName+ " will be created containing a");
    	System.out.println(" string of numbers with numbers ranging from 0 to: " + maxNumber);
    
    		
            // Create a File object 
            File myFile = new File("//home/Users/Joe/Dropbox/Programming/Java/Projects/g2.txt");
            // Tests whether the file or directory denoted by this abstract 
            //pathname exists.
            if (myFile.exists()) {
                System.out.println(myFile.getAbsolutePath() +
                        " File already exists");
    
            } else {
    		  try{
                //creates a new, empty file named by this pathname
                myFile.createNewFile();
                System.out.println(myFile.getAbsolutePath() + 
                        " File Created Successfully");
            }
    		  catch(IOException e){
    		  	System.err.println ("Caught IOException "+e.getMessage());
      }
      }
    
    	}//main
    	}//class

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Does anyone know why I am getting the error?

    Case solved. It turns out that I was missing Users.

    So the path should be ("/Users/Joe/Dropbox/Programming/Java/Projects/g2.txt")

    It compiled and created the file correctly.

    Thanks to everyone for the feedback and for allowing me to work through this little hurdle with you.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Does anyone know why I am getting the error?

    Quote Originally Posted by bigsonny View Post
    So the path should be ("/Users/Joe/Dropbox/Programming/Java/Projects/g2.txt")
    So what happens when the code is run on another computer, one that doesn't have that directory structure in place?

    Look into using a System property instead: System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Does anyone know why I am getting the error?

    Quote Originally Posted by DarrylBurke View Post
    So what happens when the code is run on another computer, one that doesn't have that directory structure in place?

    Look into using a System property instead: System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment)

    db
    Thanks DB, I'll try it.

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Tags for this Thread

Posting Permissions

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