• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

# of lines in a file?

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a command that will determine the number of lines in a .txt file?
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
enjoy
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic