• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to print the number of lines in the txt file for each txt file next to each txt file name?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package examPaper20152016;
Hi I have some code where I print out all the names of txt files from a folder/directory. I now also want to have the number of lines present in each file printed next to each txt file name but I am not sure how to do this. All help much appreciated.



Thanks
Spencer
 
Saloon Keeper
Posts: 5718
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

A crude measure would be to count the number of newline characters (\n) in the file.

As an aside: why are you using file.toString() instead of file.getName(), as in the line below that?
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Welcome to the Ranch.

A crude measure would be to count the number of newline characters (\n) in the file.

As an aside: why are you using file.toString() instead of file.getName(), as in the line below that?



Ok how do I write the syntax for this?
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Spencer Du wrote:

Tim Moores wrote:Welcome to the Ranch.

A crude measure would be to count the number of newline characters (\n) in the file.

As an aside: why are you using file.toString() instead of file.getName(), as in the line below that?



Ok how do I write the syntax for this?



Can you help me please?
 
Saloon Keeper
Posts: 3415
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Spencer,

there are some easy ways. The first is to use a Scanner, like:


Alternatively, you can use a Stream, like:

If you don't want the burden of physically loading those files, you could have some statistics about the average length of a line in a file. From that and the size of a file, you can give an estimate of the number of lines. This is not exact, of course.
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Spencer,

there are some easy ways. The first is to use a Scanner, like:


Alternatively, you can use a Stream, like:

If you don't want the burden of physically loading those files, you could have some statistics about the average length of a line in a file. From that and the size of a file, you can give an estimate of the number of lines. This is not exact, of course.



Hi is there a way to do this without having to add each txt file individually to the code?

Thanks
Spencer
 
Piet Souris
Saloon Keeper
Posts: 3415
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If you turn that code into a method that returns the nr of lines, then you can make this small addition to your code in the opening post:
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Yes. If you turn that code into a method that returns the nr of lines, then you can make this small addition to your code in the opening post:



Ok so how do i write the whole code for the program?
 
Piet Souris
Saloon Keeper
Posts: 3415
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance:
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:For instance:



Thank you so much I have a working program now with the required output being shown.
 
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use this?
 
Piet Souris
Saloon Keeper
Posts: 3415
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never heard of that class, but reading the API I far prefer the 'Files.lines(path)' method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!