• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the Best way to determine # of line in a...

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the Best way to determine the number of lines in a file to process in Java....
These files have over 100,000 lines.....
I wanted to do a count so then i can calculate and show the percentage done processed.....
currently i do 2 passes at file...
I read every record to get a total line count.
Once i have this count....i re-read file again to process each record individually
Both passes are slow.....
But on FIRST PASS.....IS there a way to figure out how many lines my file has? Or a faster way to read them for count?(on first pass)
Thanks
Java Newbie
 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you read the file size from the file system & use that instead?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are running your program in UNIX, you can try to count the number of lines using UNIX's "wc -l" command for the first run. FYI, you can use Java Runtime class to run Unix program from java.
The second loop can be done in java to do the data processing.
[ February 03, 2002: Message edited by: Donny Wi ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try the length()method in File object, this returns the number of bytes. Now as you read each line keep track of the number of bytes read so far.
Hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic