Results 1 to 14 of 14
  1. #1
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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();
    		}
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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!

  4. #4
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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

  5. #5
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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();
    		}
    	}
    }
    Attached Thumbnails Attached Thumbnails How do I make a "File does not exist" error?-file_reader3-error.png  

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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).

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

  9. #9
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Re: How do I make a "File does not exist" error?

    Quote Originally Posted by name View Post
    You were right, I did copy and paste. Right now, I need to know what I have to put for SampleException(e).
    You can change SampleException(e) to Exception(e) or just "e". like:

    Java Code:
    catch (FileNotFoundException e) 
            {
                System.err.println("FileNotFoundException: " + e.getMessage());
                throw e;
            }
    Last edited by Gilvan Justino; 01-16-2012 at 03:18 PM.

  10. #10
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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();
    		}
    	}
    }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

  13. #13
    name is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default 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?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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

  1. Replies: 2
    Last Post: 06-16-2011, 04:44 PM
  2. "Access is denied" error while file creation
    By Bharath_M in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2011, 12:23 PM
  3. Replies: 6
    Last Post: 02-10-2011, 09:55 AM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •