This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes file counting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file counting" Watch "file counting" New topic
Author

file counting

Jose Dee
Greenhorn

Joined: Jun 05, 2008
Posts: 3
hi people.

im a java beginner (started about 8 weeks ago) and i have a problem. how do you count the number of files from a folder? basically the program that im working on is reading from many different folders and for now, each time i change the name of the folder, i have to manually (from the code) change the number of files in it. is there a way so that i dont have to that everytime? any ideas?

thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
Hello "Jose D. D"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name and a last name. The first name can abbreviated by an initial, but not the last name. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, the java.io.File.listFiles method returns an array of File objects that exist in a given directory. So the length of that array would be the number of files.


Ping & DNS - my free Android networking tools app
Jose Dee
Greenhorn

Joined: Jun 05, 2008
Posts: 3
oh i might have missed reading that. but ive changed it to comply to the rules now..

and thanks. i'll try to check on that. but do you know where i might find examples on how to use it?
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hello Jose Dee ,
Welcome on javaRanch !!!

I'm not sure i'm understanding your problem.What do you mean by "manually change" ?
Are you using the java.io.File object to manage your files and directories ?

That Class has differents methods for those questions.

The method list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname


The length of the array will give you the number of file in the rerectory.

Take a look to the javadoc: File

Hope it help.
Collins
[ June 27, 2008: Message edited by: Collins Mbianda ]

SCJP 5.0 | SCWCD 1.4
Jose Dee
Greenhorn

Joined: Jun 05, 2008
Posts: 3
ok thanks. i think i got it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: file counting