Results 1 to 4 of 4
  1. #1
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    Use the File method getCanonicalPath() to print out the actual place that file names.

  3. #3
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default re:

    im confused about what you mean, can u explain please

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    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

  1. Excel File Reader
    By Subhransu in forum Advanced Java
    Replies: 6
    Last Post: 03-02-2011, 10:30 AM
  2. problem with file reader
    By Stormrage in forum New To Java
    Replies: 7
    Last Post: 05-16-2010, 12:48 AM
  3. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 09:19 AM
  4. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 08:43 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 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
  •