• 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

# of lines in a file?

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a command that will determine the number of lines in a .txt file?
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
enjoy
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic