• 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

list the files or subdirectories ina directory

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying hard for this .
please anyone help me out.
i want to list the Files or Subdirectories in a Directory upto the bottom most level without even missing a single directory or even a file from the path that i give.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way would be to use a recursive method. The class java.io.File has methods that let you get the list of the contents of a directory and methods to tell whether a File object is a file or directory.
 
vivek gaur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do i write this recursive method .
please advice me technically.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vivek gaur:
how do i write this recursive method .
please advice me technically.



Hmm, this sounds suspiciously like a homework assignment.

since a recursive method works by calling itself, in a general way, you write a method that lists the objects in a directory, examines each object in the list and, if the object represents a directory, calls itself again on that object, lists the objects in that directory, examines each object in the list, and if the object represents a directory, calls itself again ... and so forth. the tricky part is always that there must be a point of return, so that the recursion returns to the top when there are no more directories to process.

how does java handle the recursive function, when it compiles the class? i believe that some 'c' compilers unroll the recursive function and turn it into standard loops because recursive functions can be extremely inefficient in use of resources when called directly. however, in terms of comprehensibility of the original source code, they are far more useful.

well, i went to google, my home-away-from-home, solver of many homework assignments, and typed in 'java list directory recursive' and got these pages in the top 7 results:
  • Java Practices: Recursive file listing
  • Java Forums - How do I list directory contents?
  • Java Programming, Solution to Programming Exercise

  • Every time this routine finds a directory, it lists not just the name of the directory but also, recursively, the names of everything that it contains. ...
  • Java: Example - Recursive List

  • Here is an example of listing files and directories recursively. ... i
  • Traversing the Files and Directories Under a Directory (Java ...


  • so, my answer is, you should start there. then, ask specific questions about the parts you don't understand and post the code that gives you problems.

    thanks.

    mp
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic