Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-11-2010, 02:38 AM   PM User | #1
gizmo1650
Regular Coder

 
Join Date: Apr 2010
Posts: 163
Thanks: 3
Thanked 25 Times in 25 Posts
gizmo1650 is on a distinguished road
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..
gizmo1650 is offline   Reply With Quote
Old 06-11-2010, 01:20 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
You need to surround this code in a try/catch block that catches the potential "FileNotFoundException".

Code:
try
{
    doSomeStuff...
}
catch (FileNotFoundException e)
{
    doSomethingWithTheException
}
brad211987 is offline   Reply With Quote
Old 06-11-2010, 07:04 PM   PM User | #3
gizmo1650
Regular Coder

 
Join Date: Apr 2010
Posts: 163
Thanks: 3
Thanked 25 Times in 25 Posts
gizmo1650 is on a distinguished road
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?
gizmo1650 is offline   Reply With Quote
Old 06-11-2010, 07:06 PM   PM User | #4
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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..
brad211987 is offline   Reply With Quote
Old 06-11-2010, 07:57 PM   PM User | #5
gizmo1650
Regular Coder

 
Join Date: Apr 2010
Posts: 163
Thanks: 3
Thanked 25 Times in 25 Posts
gizmo1650 is on a distinguished road
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
gizmo1650 is offline   Reply With Quote
Old 06-11-2010, 08:00 PM   PM User | #6
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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.
brad211987 is offline   Reply With Quote
Old 06-11-2010, 09:48 PM   PM User | #7
gizmo1650
Regular Coder

 
Join Date: Apr 2010
Posts: 163
Thanks: 3
Thanked 25 Times in 25 Posts
gizmo1650 is on a distinguished road
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");
		}
    }
}
gizmo1650 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 AM.


Advertisement
Log in to turn off these ads.