Results 1 to 6 of 6
Thread: Scanner FileNotFoundException
- 08-13-2012, 12:19 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
Scanner FileNotFoundException
Hi I'm new to scanners and i'm getting this error:
I am trying to read a text file and I think the problem might be the way i'm creating my scanner, but i'm not sure..Java Code:Error: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Have i done something wrong? I have also tried using an absolute path, but i got the same error.. :(Java Code:String fileName = "Save.txt"; Scanner scan = null; File file = new File(fileName); scan = new Scanner(file);
Thanks.
- 08-13-2012, 12:43 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Scanner FileNotFoundException
The compiler doesn't know anything about paths, whether absolute or not. All it knows is that possibly a file could not be found and the Scanner might throw an Exception and your code doesn't anticipate on it, nor does it state that it lets pass the Exception; either catch the Exception with a try ... catch block or tell the compiler that your method lets this Exception pass ('throws' in the header in of the method).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-13-2012, 01:31 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
- 08-13-2012, 02:16 PM #4
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Scanner FileNotFoundException
There is one problem, you know that your .txt-file exist in your directory, but does your Scanner know this?
Is it in your current working directory?
You can also try to put a dot before your file.txt-path, like
Java Code:String fileName = "./Save.txt"; Scanner scan = null; File file = new File(fileName); scan = new Scanner(file);
- 08-13-2012, 02:30 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Scanner FileNotFoundException
You need to handle the exception.
The Scanner(File) constructor throws a FileNotFoundException, so you have to handle it in your code, as Jos says.
Here's the exceptions tutorial.Please do not ask for code as refusal often offends.
- 08-13-2012, 03:20 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
Help | FileNotFoundException: db.properties
By Maxpain43 in forum NetBeansReplies: 0Last Post: 07-22-2011, 10:35 AM -
Help with FileNotFoundException
By Beginner in forum New To JavaReplies: 11Last Post: 11-22-2010, 04:44 PM -
FileNotFoundException
By ProgrammingPup in forum Advanced JavaReplies: 4Last Post: 12-30-2009, 01:29 AM -
filenotfoundexception :@
By wildheart in forum New To JavaReplies: 2Last Post: 04-25-2009, 09:56 AM -
FileNotFoundException
By PeonLover in forum New To JavaReplies: 1Last Post: 12-26-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks