• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

file counting

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks. i think i got it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic