The moose likes Beginning Java and the fly likes counting files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "counting files" Watch "counting files" New topic

counting files

Steve Matthews

Joined: Apr 18, 2002
Posts: 6
Dear members
I would like to know of an easy way to count the number of files (not directories) within a given folder. Is there such a method available within the 'file' class ?
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
bigSteve -
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I know the File class has methods that allow you to do that. You will create a File object and have it reference the directory you want to list, and then loop through the object listing all the file. You can even filter for specific extensions if you need to. Check the API for more details.

So much trouble in the world -- Bob Marley
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
If I coould remember properly there is a metod listFiles which gives the array of files but it includes the directory also you can seggrate them by using either isDirectory or isFile method..I am not sure if it counts for the files 7 directory of its subfolder or not.
I agree. Here's the link:
subject: counting files
It's not a secret anymore!