Results 1 to 4 of 4
  1. #1
    BoomPony is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Simple file reader won't work in eclipse

    I'm relatively new to java, using eclipse, and I have some code which I believe should work but just outputs "file.txt failed to open" every time I run it. I've stored the file "file.txt" in the root folder of my project, on the same level as src, bin and .settings. Although I've tried copying it literally everywhere in case this was the wrong place. here is my code

    Java Code:
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
     
    public class JavaIO
    {
      public static void main( String [] args )
      {
    	  String filename = "file.txt";
    	  
        try
        {
           FileReader fr = new FileReader(filename);
           BufferedReader br = new BufferedReader(fr);
     
           String str = br.readLine( );
     
           while(str != null) 
           {
              System.out.println(str);
              str = br.readLine( );
           }
     
           br.close( );
        }
     
        catch( IOException e)
        {
          System.out.println( filename + " failed to open" );
        }
      }
    }
    Any ideas?
    Thanks in advance :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To see where the user directory is, where Java is looking for the file, run this line in your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    Your file will need to have a path relative to this directory.

  3. #3
    BoomPony is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks Fubarable that's sorted now

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. problem with file reader
    By Stormrage in forum New To Java
    Replies: 7
    Last Post: 05-16-2010, 12:48 AM
  2. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 09:19 AM
  3. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 08:43 PM
  4. A simple DOM reader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:24 AM
  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
  •