Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Get # of lines in text file

Thread view: 
Jo - 09 Apr 2004 16:36 GMT
How do I get the number of lines in a text file?
Collin VanDyck - 09 Apr 2004 16:55 GMT
> 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.
Jo - 09 Apr 2004 22:26 GMT
> > 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
Chris Smith - 09 Apr 2004 23:17 GMT
> 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));

   ...

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 10 Apr 2004 00:33 GMT
>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 - 09 Apr 2004 19:31 GMT
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.