• 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

Find the total number of lines (sum) in all files in a directory

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


How to find the total number of lines in all files in a directory in java? I have written the below code which fetches number of lines of each files, where I need to find total number of lines in all files in "Test" directory. How to refactor the code get the total number of lines in all files in a directory ?

Please clarify.

Thanks.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rithanya Laxmi wrote:How to find the total number of lines in all files in a directory in java? I have written the below code which fetches number of lines of each files, where I need to find total number of lines in all files in "Test" directory. How to refactor the code get the total number of lines in all files in a directory ?


Answer: Not easily, because not all files have lines (eg, a JPEG file); and there is no fullproof method for working out what type of file you have. If you are happy to assume that all .java files are OK, then you might want to look at LineNumberReader.

However, my advice: Get it right for ONE file; then start worrying about directories.

Winston
 
Rithanya Laxmi
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I able to get the total lines of individual files , as shown in the code, but now i need to get the sum of total lines of all the files in the "Test" directory. Please provide your input.
 
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
My advice:

StopCoding. You're trying to write the code before you've thought through the problem.

You want to separate out the discrete components of your problem into their own methods. Some of the things your program should be able to do:

1) Given a file name, a method that can return the number of lines in the file
2) Given a directory, generate a list of files contained in that directory
3) Given a list of files, iterate through it, looking at each file name one at a time.
4) keep a running sum of a bunch of numbers, being given them one at a time.

I think that once you figure out how to do each of the above, and then start wiring them together, you'll find a) a few more tasks/methods that need to be written, and b) you'll end up with a working program that is readable and debuggable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic