Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts

    reading an external txt file

    i'm trying to read the contents of an external file in my program, but java can't find the file. the line it marks is FileReader fr = new FileReader( "C:\\Euler_18_triangle.txt" ) ; speccificlly the n in new. And the error is: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown there is a file called Euler_18_triangle.txt in the top level of my c drive
    Last edited by gizmo1650; 06-11-2010 at 11:20 PM.

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    You need to surround this code in a try/catch block that catches the potential "FileNotFoundException".

    Code:
    try
    {
        doSomeStuff...
    }
    catch (FileNotFoundException e)
    {
        doSomethingWithTheException
    }

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    that solves that error, but when i call the variables based of off the text file it says they don't exist. i think the problem is how i'm referencing the file FileReader fr = new FileReader( "C:\\Euler_18_triangle.txt" ) ; any ideas?

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    As long as that file exists at the root of your C drive it looks OK to me. Post your code so it will be easier for me or anyone else to help you.

    It is nearly impossible for me to guess the error simply based on a description, post any information that is related including code, error messages, stack trace etc..

  • #5
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    current code:
    Code:
    import java.io.*;
    public class Euler_18 {
        public static void main(String[] args) {
    	
    		try{
    			FileReader fr = new FileReader( "C:\\Euler_18_triangle.txt" ) ;
    			BufferedReader reader = new BufferedReader( fr ) ;
    			String line = null ;
    			}	
    		catch (FileNotFoundException e){
    			System.out.println("cant find file");
    		}
    
    		while( ( line = reader.readLine() ) != null ){
    			System.out.println( line ) ;
    		}
    	
    		
    		
    		
    		
        }
    }
    when i compile it says it can't find the variables line or reader

  • #6
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    It is a scope problem.

    Once your code is finished with the try block, line and reader do not exist any longer and thus you cannot use them in your later defined while loop.

  • #7
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    Thanks, working code:
    Code:
    import java.io.*;
    public class Euler_18 {
        public static void main(String[] args) {
    		FileReader fr = null;
    		BufferedReader reader = null;
    		String line = null ;
    		try{
    			fr = new FileReader( "C:\\Euler_18_triangle.txt" ) ;
    			reader = new BufferedReader( fr ) ;
    			}	
    		catch (FileNotFoundException e){
    			System.out.println("cant find file");
    		}
    
    		try{
    			while( ( line = reader.readLine() ) != null ){
    				System.out.println( line ) ;
    			}
    		}
    		catch (java.io.IOException e){
    			System.out.println("IOExeption");
    		}
        }
    }


  •  

    Posting Permissions

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