This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes # of lines in a file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "# of lines in a file?" Watch "# of lines in a file?" New topic
Author

# of lines in a file?

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Is there a command that will determine the number of lines in a .txt file?
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Are you wanting just a commandline command?
wc -l myfile.txt (if you are using Unix/Linux)
or do you want to use something in java.io.* package or similar?
[ November 13, 2003: Message edited by: James Swan ]
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
Something in a java.io package, something that I would be able to write into my program, not a command line command. Sorry that I didn't clarify

[ November 13, 2003: Message edited by: Brandi Love ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

do you need it before you do anything else? you can always read the file one line at a time, and keep a counter...
I've never heard of anything that would do what you seem to be asking for, but i'm not willing to say there absolutly is not...
how's that for not answering the question???


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
how would I do that? With a while loop, or perhaps a for loop?
I need this for a program that serches for a string within some text in a text file, and when it is found gives the location of the line, and column in which the text is located.
[ November 13, 2003: Message edited by: Brandi Love ]
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
enjoy
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403

I need this for a program that serches for a string within some text in a text file, and when it is found gives the location of the line, and column in which the text is located.

In that case you could also look into using java.io.LineNumberReader (which is a subclass of BufferedReader).
A basic algorithm would be:
- read each line of the file into a String
- compare String against criteria (String.indexOf("my_criteria"))
- if a match, record line number from LineNumberReader.getLineNumber(), and record position of match in String (indexOf)

James.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: # of lines in a file?
 
Similar Threads
WA #1.....word association
Ok, I'm stumped (Servlet-4a)
When executing a JSP i get "Code too large for try statement"
Apache Ant help
DB2 Script File