Results 1 to 4 of 4
- 04-22-2011, 01:37 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
java file reader, jgrasp can't find the file
Hi guys,
i have the following code to read a file, everything is fine and the code compiles, but it cannot find the file to read it.
I'm using j-grasp and both the file and program are saved in the same folder.
the code is:
import java.util.Scanner;
public class Project8
{
public static void main(String[]args) throws Exception
{
java.io.File file = new java.io.File("text.txt");
Scanner input = new Scanner(file);
while (input.hasNext())
{
String year = input.next();
String month = input.next();
System.out.println("Year: "+year +"\n Month: "+month);
}
input.close();
}
}
the error i get when i try to run the program:
----jGRASP exec: java Project8
Exception in thread "main" java.io.FileNotFoundException: text.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
at java.util.Scanner.<init>(Scanner.java:621)
at Project8.main(Project8.java:14)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 04-22-2011, 01:47 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Use the File method getCanonicalPath() to print out the actual place that file names.
- 04-22-2011, 01:56 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 14
- Rep Power
- 0
re:
im confused about what you mean, can u explain please
- 04-22-2011, 02:06 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
The runtime error message you get is saying that the file named by 'new java.io.File("text.txt")' does not exist. The runtime will not lie: that file really does not exist.
So the question is "what is the file that is named by file?" ANd I am suggesting that you print out the full name (including path etc) of file. There is a File method - getAbsolutePath() - that you should read about and use.
The idea is that once you have a clearer idea about the file you are naming with 'new java.io.File("text.txt")' you can figure out how change the "text.txt" bit so that it names a file that really does exist. (Alternatively you can move the file itself to the location that getAbsolutePath() will reveal is being used to look for the file.)
Similar Threads
-
Excel File Reader
By Subhransu in forum Advanced JavaReplies: 6Last Post: 03-02-2011, 09:30 AM -
problem with file reader
By Stormrage in forum New To JavaReplies: 7Last Post: 05-15-2010, 11:48 PM -
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks