Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file counting

 
Jose Dee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Dee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jose Dee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks. i think i got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic