Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

# of lines in a file?

 
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?
 
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 ]
 
lowercase baba
Posts: 12871
62
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.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic