• 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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For instance:
 
Spencer Du
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use this?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never heard of that class, but reading the API I far prefer the 'Files.lines(path)' method.
reply
    Bookmark Topic Watch Topic
  • New Topic