Get # of lines in text file

Discussion in 'Java' started by Jo, Apr 9, 2004.

  1. Jo

    Jo Guest

    How do I get the number of lines in a text file?
     
    Jo, Apr 9, 2004
    #1
    1. Advertising

  2. "Jo" <> wrote in message
    news:c56fuk$2p1bqn$-berlin.de...
    > How do I get the number of lines in a text file?
    >


    Try creating a LineNumberReader and read the entire file line by line and
    keep count.
     
    Collin VanDyck, Apr 9, 2004
    #2
    1. Advertising

  3. Jo

    Roedy Green Guest

    On Fri, 9 Apr 2004 16:36:55 +0100, "Jo" <> wrote or
    quoted :

    >How do I get the number of lines in a text file?


    do readlns till eof counting.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 9, 2004
    #3
  4. Jo

    Jo Guest

    "Collin VanDyck" <> wrote in message
    news:zHzdc.1828816$...
    > "Jo" <> wrote in message
    > news:c56fuk$2p1bqn$-berlin.de...
    > > How do I get the number of lines in a text file?
    > >

    >
    > Try creating a LineNumberReader and read the entire file line by line and
    > keep count.


    I did this but when I use LineNumberReader it seems to make
    BufferedReader.readLine() return null. Any ideas how I can "reset" the
    BufferedReader so it goes back to the start of the file (and it's not the
    reset() method).

    If I remove the LineNumberReader() stuff in the while loop then the
    BufferedReader works fine.

    <code>

    public Record[] readScoreboard() throws IOException {
    FileReader fr = new FileReader(_scores_file);
    BufferedReader br = new BufferedReader(fr);
    LineNumberReader lnr = new LineNumberReader(fr);
    String str_line;
    int line_count = 0;
    boolean flag = true;
    String str_count;

    while (flag) {
    str_count = lnr.readLine ();
    if (str_count == null) {
    flag = false;
    }
    else {
    line_count++;
    }
    }

    //System.out.println("lines: " + line_count);
    int t = 0;
    int x = 0;
    int y = 0;
    int m = 0;

    for (int i = 1; i <= line_count; i++) {
    System.out.println("here");
    try {
    str_line = br.readLine();
    System.out.println(str_line); // this displays "null"

    }
    catch (NumberFormatException e) {
    JOptionPane.showMessageDialog(new JDialog(), e.getMessage());
    }
    }
    catch (NullPointerException e) {
    //JOptionPane.showMessageDialog(new JDialog(), e.getMessage());
    }
    }

    //input done, so close the stream
    br.close();

    return _records;
    }

    </code>
     
    Jo, Apr 9, 2004
    #4
  5. Jo

    Chris Smith Guest

    Jo wrote:
    > I did this but when I use LineNumberReader it seems to make
    > BufferedReader.readLine() return null. Any ideas how I can "reset" the
    > BufferedReader so it goes back to the start of the file (and it's not the
    > reset() method).


    A Reader is a one-shot thing. To start over, you need to create a new
    FileReader to read from _scores_file.

    Incidentally, you're mis-using LineNumberReader. Just do this:

    LineNumberReader lnr = new LineNumberReader(
    new FileReader(_scores_file));
    try
    {
    lnr.setLineNumber(1);

    String line;
    while ((line = lnr.readLine()) != null);

    line_count = lnr.getLineNumber();
    }
    finally
    {
    lnr.close();
    }

    And then,

    BufferedReader br = new BufferedReader(
    new FileReader(_scores_file));

    ...

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 9, 2004
    #5
  6. Jo

    Roedy Green Guest

    On Fri, 9 Apr 2004 16:17:56 -0600, Chris Smith <> wrote
    or quoted :

    >A Reader is a one-shot thing. To start over, you need to create a new
    >FileReader to read from _scores_file.

    remember to close the old reader.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mullin
    Replies:
    4
    Views:
    5,489
    Harald
    Jun 12, 2005
  2. Joe Wright
    Replies:
    0
    Views:
    526
    Joe Wright
    Jul 27, 2003
  3. Murali
    Replies:
    2
    Views:
    575
    Jerry Coffin
    Mar 9, 2006
  4. Replies:
    17
    Views:
    292
    Tad McClellan
    Mar 10, 2006
  5. Cah Sableng
    Replies:
    0
    Views:
    240
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page