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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file counting" Watch "file counting" New topic

file counting

Jose Dee

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

Joined: Mar 22, 2005
Posts: 42965
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 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.
Jose Dee

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 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.
[ June 27, 2008: Message edited by: Collins Mbianda ]

SCJP 5.0 | SCWCD 1.4
Jose Dee

Joined: Jun 05, 2008
Posts: 3
ok thanks. i think i got it.
I agree. Here's the link:
subject: file counting
It's not a secret anymore!