Results 1 to 4 of 4

Thread: reading from file[Resolved]

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved reading from file[Resolved]

    How can you read a certain line from a file without wasting your time processing each line like this:
    Code:
    try {
            BufferedReader in = new BufferedReader(new FileReader("infilename"));
            String str;
            while ((str = in.readLine()) != null) {
                process(str);
            }
            in.close();
        } catch (IOException e) {}
    Like, if I had a list of 5000 words, and I wanted to skip to the 1234th word, how would I do that? I know I could use a counting variable in the above example, but I wouldn't want to keep reading everyline like that untill I got to the right one.
    Last edited by NoteMe; May 25th, 2005 at 02:48 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: reading from file

    Perhaps java.io.LineNumberReader?. Maybe you can use setLineNumber(int lineNumber) to set the line number then readLine() to read that exact line.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: reading from file

    Nope sorry. According to the docs.
    Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

  4. #4

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: reading from file

    Thanks. Actually, I think what I was going to do was a bad idea. Instead, I read all 5000 words from the file into an arraylist, at runtime. That way I didn't have to deal with the file anymore, and that worked out well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width