Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Reading a specific line from a file

    Hi!
    I know how to read a line from a txt file by using a buffered reader

    String line = "";
    String tempLine = "";
    BufferedReader bufferedReader = new BufferedReader(new FileReader(filename));

    // use the readLine method of the BufferedReader to read one line at a time.
    line = bufferedReader.readLine();

    But I dont know how to read a specific line in a txt file. (Example : Read line no 3 or 5 or 150 ... )

    How do I do that?

    Thank you

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    There is no "out of the box" way to do it. Read the lines in until you reach the line you want.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    There's also the LineNumberReader class, it extends the BufferedReader class and does the line counting for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by JosAH View Post
    There's also the LineNumberReader class, it extends the BufferedReader class and does the line counting for you.
    Cool! I've never seen that class before. I feel like I have done something with my day now, thanks for that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KevinWorkman View Post
    Cool! I've never seen that class before. I feel like I have done something with my day now, thanks for that.
    That's what I don't like about the API documentation format: for every class you have to check the superclasses of the class but you also have to pay attention to the 'direct known subclasses' ... I don't have an alternative for that little issue though ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks alot for the replies!

    I have tried using LineNumberReader, but it also always reads the first line? Am I doing something wrong?

    I am trying to get a random line from the file, here is what I tried


    String line;
    Random randomGenerator = new Random();
    int lineNo = randomGenerator.nextInt(1000);
    try
    {
    LineNumberReader lineNumberReader = null;
    //Get a new random puzzle from puzzle database
    lineNumberReader = new LineNumberReader(new FileReader("Puzzles\\easy.txt"));
    lineNumberReader.setLineNumber(lineNo);
    line = lineNumberReader.readLine();
    display.append("Difficulty : Easy - Puzzle no : " + lineNo + " \n");
    display.append("Puzzle : " + line + "\n");

    As I set line number to a random number, it is supposed to read a random line every time, but it always reads the first line... Any ideas why this happens?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    From the javadoc:
    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().
    It's for keeping tabs on the line number, not for changing the actual line you;re looking at.

    You still need to loop through until the lineNumber reaches the one you want.

Similar Threads

  1. Write to a specific line in a file
    By phantom06 in forum New To Java
    Replies: 1
    Last Post: 02-05-2011, 07:32 PM
  2. Reading specific portions of text file
    By Plex in forum New To Java
    Replies: 1
    Last Post: 11-09-2010, 07:45 PM
  3. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 11:40 PM
  4. writing to specific line in text file
    By mickmos in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 02:01 PM
  5. Writing To A Specific Text File Line
    By mokonji in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 09:13 PM

Posting Permissions

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