This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes What is the Best way to determine # of line in a... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the Best way to determine # of line in a..." Watch "What is the Best way to determine # of line in a..." New topic
Author

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

Rudolph Ten
Greenhorn

Joined: Jan 23, 2002
Posts: 9
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
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 566
Can't you read the file size from the file system & use that instead?


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Donny Wi
Greenhorn

Joined: Jan 24, 2002
Posts: 13
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 ]

Donny Widjaja
Les Dsouza
Greenhorn

Joined: Jan 29, 2002
Posts: 27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the Best way to determine # of line in a...