This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read subdirectories and count files?

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i need some help on counting total files in each subdirectories . I already has a method reading the directories, but not sure how to count file for each subdirectories. I need to have the output like this. For example I read from c:\temp

result will like this:
c:\temp\one\ 3 (assume this sub-directory have 3 files)
c:\temp\one\two 10 (assume this sub-directory have 10 files)
c:\temp\one\two\three 4 (assume this sub-directory have 4 files)

You help is greatly appreciated.
Regards.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what's this getFileListing() method you're calling? It looks like you're missing a method. And what happens when you run the code you have now? How are the results different from what you expect?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for a late response, i was tight up with something else, and now I can revisit the problem.

the getFiles( File aStartingDir ) // giving me a list of directory including the directory and files name , so how do I do the count for how many files that are in the sub-directory under the parent directory
This is the out put of the getFiles method, How do I count each of the line and display the total files under each directory?
c:\temp\dir1\test1.txt
c:\temp\dir1\test2.txt
c:\temp\dir1\dir2\text3.txt
c:\temp\dir1\dir2\text4.txt
c:\temp\dir1\dir2\dir3\test5.txt

so the result should be like:
c:\temp\dir1\ 2 files
c:\temp\dir1\dir2\ 2 files
c:\temp\dir1\dir2\dir3\ 1 file

Any help or hints is greatly appreciated. Regards
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to count the totol files in each directory and print out,

Please help ! Thanks in advance

I do: java ListAllFiles c:\temp -----> it is giving NullPointerException?

 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is always polite to share the exception stack trace as that contains the line number the exception occurs on.
One very useful debugging technique is to sprinkle System.out.println() statements throughout one's code. I always like to print out method calls and their parameters as well as important variables that I'm examining, like, say, if a file is a directory or not. Why don't you try this technique and share the results with us?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the stack trace contains the line number of the exception, but that line number won't mean anything to us because we don't have your complete file. We can't tell what the line numbers mean. But if your stack trace starts off with

that tells you to look at ListAllFiles.java, line 42. Tell us which line that is, and we might be able to help.

Also, I don't think this will explain your NullPointerException, but here's another problem:

When you call oneFile.getName(), you get only the name of the file, not the directory it's in. So the countFilesIn() methods won't be able to find the files, because it doesn't know where they are. I recommend you change the method to take a File parameter rather than a String - that way the File object still has all the info you need to find the file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic