Results 1 to 14 of 14
- 01-12-2012, 03:27 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
How do I make a "File does not exist" error?
Hi,
I am making a java app that reads .txt files for a school assignment. The program is designed to first ask the user to input the location of the file to read (e.x. Libraries\Documents\file.txt) and display the file's contents to the user. I have managed to make the program ask for the file location and got it to read the file's contents. The only issue I am having with it is if I input a file that doesn't exist, the program crashes. I would like to make the program display an error message to the user if they input a file name and location that doesn't exist (e.x. "That file doesn't exist" or "Please try again"). Could someone please help?
Thanks,
name
Java Code:import java.io.*; import java.util.*; public class file_reader3 { public static void main(String[] args) throws IOException { BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println(" "); System.out.println("Welcome to File Reader!"); System.out.println(" "); System.out.println("Please Insert File Name and Location"); String file = in.readLine(); System.out.println(" "); String line; in = new BufferedReader(new FileReader(file)); line = in.readLine(); while(line != null) { System.out.println(line); line= in.readLine(); } } }
- 01-12-2012, 03:57 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: How do I make a "File does not exist" error?
Catch the IOException and deal with it accordingly. See
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
- 01-12-2012, 04:00 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I make a "File does not exist" error?
Instead of throwing that IOException, handle it in a try/catch block.
Loop round that block until a File hjas been successfully selected (or some other exit condition has occurred).
ETA: Curse! Ninja'd!
- 01-12-2012, 04:16 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
I will try reading that link you sent me doWhile, thanks. I shall post again if it worked or didn't
- 01-13-2012, 04:02 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
Hi again,
I looked at the link you sent me, and tried to use the Try, Catch, and Finally statements, but after lots of trial and error it still doesn't work
! I never got it to compile. Could you please check it to see what I might have done wrong? I will attach the new code and a picture of the error message when I compile.
Thanks,
name
Java Code:import java.io.*; import java.util.*; public class file_reader3 { public static void main(String[] args) throws IOException { //Start input stream BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); //Greet user System.out.println(" "); System.out.println("Welcome to File Reader!"); System.out.println(" "); //Ask for file System.out.println("Please Insert File Name"); String file = in.readLine(); System.out.println(" "); //Start file reader (with try statement) try { String line; in = new BufferedReader(new FileReader(file)); line = in.readLine(); } //Catch Statement catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); throw new SampleException(e); } //Finally Statement finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } //Read file while(line != null) { System.out.println(line); line= in.readLine(); } } }
-
Re: How do I make a "File does not exist" error?
One error at at time:
The compiler can't find a SampleException class with constructor that takes a FileNotFoundException parameter. Do you have a class of this name, and what do its constructors look like?
Next, you're using a variable out that doesn't exist. Where do you declare this variable in your class above? It looks like you created the finally block by doing a copy/paste without thinking and that never works.
- 01-16-2012, 03:02 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
You were right, I did copy and paste. Right now, I need to know what I have to put for SampleException(e).
- 01-16-2012, 03:15 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I make a "File does not exist" error?
Why are you wrapping the FileNotFoundException in a SampleException?
Answer that and we may be able to point you in the right direction.
- 01-16-2012, 03:15 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 19
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
Last edited by Gilvan Justino; 01-16-2012 at 03:18 PM.
- 01-16-2012, 03:41 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
Thanks, but now when I enter the name of the file I want to read, it displays "Closing File Reader", reads the first line, then crashes.
example:
Please input file name
C:\name\my documents\message.txt
Closing File Reader
message text
Exception in thread "main" java.io.IOException: Stream closed
Could someone please help fix this? I will attach the updated code below.
Thanks,
name
Java Code:import java.io.*; import java.util.*; public class file_reader3 { public static void main(String[] args) throws IOException { //Start input stream BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); //Greet user System.out.println(" "); System.out.println("Welcome to File Reader!"); System.out.println(" "); //Ask for file System.out.println("Please Insert File Name"); String file = in.readLine(); System.out.println(" "); String line; //Start file reader (with try statement) try { in = new BufferedReader(new FileReader(file)); line = in.readLine(); } //Catch Statement catch (FileNotFoundException e) { System.err.println("FileNotFoundException: " + e.getMessage()); throw(e); } //Finally Statement finally { if (in != null) { System.out.println("Closing File Reader"); in.close(); } else { System.out.println("File Reader not open"); } } //Read file while(line != null) { System.out.println(line); line= in.readLine(); } } }
- 01-16-2012, 03:47 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I make a "File does not exist" error?
That's because in.close() can (and is) throwing an exception.
That close() should be wrapped in it's own try/catch block, because the exception caused by problems with close() is hiding the original exception.
- 01-16-2012, 03:48 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I make a "File does not exist" error?
By the way, so you really want to be using the same variable name for your Scanner input as your file input?
Strikes me as a bit confusing.
- 01-16-2012, 04:00 PM #13
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How do I make a "File does not exist" error?
I removed the in.close() line and it works perfectly
. the only issue left is that the Closing File Reader line still shows when I read the file!
example:
Please input file name
C:\name\my documents\message.txt
Closing File Reader
message text line 1
message text line 2
How can I prevent Closing File Reader from appearing in my program (except when there is an error), and is it a good idea to remove the in.close() line?
- 01-16-2012, 04:19 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How do I make a "File does not exist" error?
You want to close the file.
You should never leave it up to the underlying Java code to close them for you.
Just do what I said and wrap the close() call in a try.catch block.
As for the text being output, well that's exactly what you asked for...the finally block is always executed, so that text will be output if 'is' is not null.
Of course I don't think you actually want to close that stream before you;ve actually read anything. So you'll want to move your reading code as well.
Similar Threads
-
Error "org.apache.commons.logging package does not exist" appeared
By vjoshi in forum Java ServletReplies: 2Last Post: 06-16-2011, 04:44 PM -
"Access is denied" error while file creation
By Bharath_M in forum Advanced JavaReplies: 4Last Post: 04-10-2011, 12:23 PM -
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks