Results 1 to 6 of 6
  1. #1
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default reading file from a path

    Hi,
    i am trying to read file from a path like this "/var/tmp/twl06.txt"
    i am getting error.
    anyone please suggest how to do it.

    thanks in advance

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

    Default

    How are you trying to read it, and we can tell you perhaps where you're going wrong. Also, if you get an error (ever) and need to ask a question about it here, please post the error message completely and indicate in your code where the error is occurring, usually by placing a comment like so:
    Java Code:
    for (int i = 0; i <= myArray.length; i++) {
       System.out.println(myArray[i]); // **** array index out of bounds occurs here ****
    }
    [/code]

  3. #3
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Java Code:
     BufferedReader br = new BufferedReader(new FileReader("tw106.txt"));
                while((br.readLine()) != null)
                {
                    for(int i=0; i<178690; i++)
                    {
                        wordList[i] = br.readLine();
                    }
                }
    instead of the file there...i want to read it from the path given above

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    You need to create a file object:
    Java Code:
    BufferedReader br = new BufferedReader(new FileReader(new File("tw106.txt")));

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

    Default

    I don't think that the problem is the one outlined by quad above as FileReader has a constructor that takes String and likely creates a File within the constructor. The problem is likely that the file is not located where he's looking. To the original poster, run this in your program:

    Java Code:
    System.out.println(System.getProperties("user.dir"));
    This will tell you where the JVM is looking for your file.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Whoops, you are correct. Thanks! I've been doing things so long a certain way, I sometimes miss things :D

Similar Threads

  1. find file with * on the path
    By itaipee in forum New To Java
    Replies: 7
    Last Post: 07-08-2009, 08:05 AM
  2. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 03:32 PM
  3. How to set Image file path
    By anil.bharadia in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2009, 09:10 PM
  4. Platform dependent File Path....
    By sanjayksaini in forum Advanced Java
    Replies: 7
    Last Post: 11-30-2008, 03:12 AM
  5. file path
    By Arsench in forum New To Java
    Replies: 7
    Last Post: 10-30-2008, 05:39 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
  •