my dog learned polymorphism*
The moose likes Java in General and the fly likes How can I find out how many lines there are? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I find out how many lines there are?" Watch "How can I find out how many lines there are?" New topic
Author

How can I find out how many lines there are?

ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
Hello all,
I have this code that reads in a file checks for commas and then adds all the appropiate text to a vector.


But how can find out how many lines of text there is in my file?
for example say these 2 lines where the the 2 lines in my work.csv file, I want to be able to get the output as 2.

Thankis
Lol
[ January 05, 2004: Message edited by: ben riches ]
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
You may be able to search for \n or \r depending on what your data file is using. That should tell you how many lines there are. Example... Number of lines = number of \r + 1
-Dale


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Gillian Bladen-Clark
Greenhorn

Joined: Dec 13, 2003
Posts: 18
The StringTokenizer is designed to do all the work of deciding what is a word separator for you - in your case, a comma.
I suggest you read line by line then get the tokens. e.g.

And the code for the file reading class is:

[ January 05, 2004: Message edited by: Gillian Bladen-Clark ]
John Ipe
Greenhorn

Joined: Dec 29, 2003
Posts: 25
I think counting the number of times buff.readLine() returns a non-null value will tell you how many lines there are, cos readLine reads the file one line at a time. So all you may need is a counter that counts how many times your readLine executed without returning null.
Charlie Havener
Greenhorn

Joined: Jul 07, 2003
Posts: 8
Use the excellent Ostermiller utilities to read csv files, even the sicko Microsoft Excel csv files.
Ostermiller Utilities
Check it out. It is the greatest thing since sliced bread. It works like this.
FileReader in = new FileReader( sourceFile);
ExcelCSVParser parser = new ExcelCSVParser(in);
String[][] values = parser.getAllValues(); // values[ rows ][ columns ]
int numCols = values[0].length;
int numRows = values.length;
The numRows will be your number of lines.
Charlie www.cdhconsult.com


Charlie
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
Good link man! Those Ostermiller utils look real cool!! thanks


Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or you could simply use the java.io.LineNumberReader


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Ilja Preuss:
Or you could simply use the java.io.LineNumberReader

In addition, I would like to mention that using an integrated class results better performance than using a third-party package like Ostermiller utilities... but I'm not sure whether there are good features added in that utilities... If so, just go ahead to use that third-party package... Otherwise consider about the performance of ur application as well...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ko Ko Naing:

In addition, I would like to mention that using an integrated class results better performance than using a third-party package like Ostermiller utilities...

Why would that be???
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Ilja Preuss:

Why would that be???

I mean I support people, who use the ready-made classes like java.io.LineNumberReader, rather than re-inventing the wheel or using the re-invented wheel in the third-party packages...
Coz Sun also recommend developers to use the classes in the Java Standard API, instead of inventing the same thing, especially when we take the SCJD exam... They extract the marks, if we are trying to re-invent the wheel...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I also think that reinventing the wheel is stupid, and using existing libraries will certainly improve development performance.
I somehow did understand you to mean that using Sun libraries could somehow improve *application* performance. Sorry if I misunderstood you.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Ilja Preuss:
I somehow did understand you to mean that using Sun libraries could somehow improve *application* performance. Sorry if I misunderstood you.

It's ok. Sometimes I can feel that my language usage is not clear enough to make other people understand well what I want to express....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I find out how many lines there are?