• Post Reply Bookmark Topic Watch Topic
  • New Topic

list the files or subdirectories ina directory

 
vivek gaur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
how do i write this recursive method .
please advice me technically.
 
Michael Powe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!