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?
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:
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?
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 :)
Re: Does anyone know why I am getting the error?
Quote:
Originally Posted by
rougeking
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
Re: Does anyone know why I am getting the error?
Quote:
Originally Posted by
bigsonny
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
Re: Does anyone know why I am getting the error?
Quote:
Originally Posted by
DarrylBurke
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.
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.
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
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.
Re: Does anyone know why I am getting the error?
Quote:
Originally Posted by
bigsonny
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
Re: Does anyone know why I am getting the error?
Quote:
Originally Posted by
DarrylBurke
Thanks DB, I'll try it.