plzz help me...........
i want to list all the sub directories,files(including subdirectory files) in a particular drive(C,D,E,etc).

Recommended Answers

All 3 Replies

So what's stopping you?
It's a simple recursive descent, isn't it? Start with a File. If it's not a directory, you're done: return this file. If it is a directory, add its contents to your list. How do you do that? For each file in the directory, if it's not a directory, it's a file: return it. If is a directory, add its contents to the list, by the same procedure...

plzzzz send me the code for this......

So what's stopping you?
It's a simple recursive descent, isn't it? Start with a File. If it's not a directory, you're done: return this file. If it is a directory, add its contents to your list. How do you do that? For each file in the directory, if it's not a directory, it's a file: return it. If is a directory, add its contents to the list, by the same procedure...

No, won't do that. But start by looking at java.io.File - it represents either a file or a directory, and it has methods to tell you which it is. If it's a directory, it can also list its contents.
See if you can just list the contents of your root directory, by name and with some indication of whether each is a file or a directory. Once you've done that, it's easy. The hard part, it sounds like, is going to be in wrapping your head around the idea of recursion.

Here's some pseudo-code to help you out:

public void readFile(File root)
  for each File f in root
    {
      if (f is a file)
         add f to root's contents and return
      if (f is a directory)
      {
         add f to root's contents
         readFile(f)
      }
   }

That's pretty much it: recursive descent. If you're not clear on how this works, think of a file structure like this - file names in UPCASE, directories in lower:

root : a b c D
a: e F G
b: H I j
c: K L M
e: N O p
j: Q R
p: S T

You start with root. It's got three directories and a file. So you look at them in turn, starting with a.
  a has a directory and two files. You look at them in turn, starting with e.
    e has two files and a directory, add e to a's list and look at each of them 
    in turn, starting with N
      N is a file. Add it to e's list of files, and you're done with that
      O is a file. Add it to the list and move on. 
      p is a directory, so add it to e's list and look in it.
        S is a file, add it to p's list.
        T is a file, add it to p's list, and you're done with p.
      (now we've looked at everything in e, so we can return to the contents of a)
    F is a file, so add it to the list of a's contents
    G is a file, so add it to the list of a's contents
    (now we've looked at everything in a, so we can go back up and look at b, but 
    I hope you get the idea...)

When you're done, you'll have a representation of the file structure in memory - you can print it to the screen, write it to disk, whatever you like. The representation will be hierarchical - the only access to T will be through p, which you can only get to through e, which you can only get to through a, but walking this sort of tree is pretty simple once you get the hang of it.

I've left some things for you to figure out - for example, what's labelled as a "File" here will have to be some sort of node wrapped around a file. Basically:

public class FileNode
{
  private File f; // the file
  private FileNode parent; //what points to this?
  private ArrayList<FileNode> children;  // what does this point to?
}

I hope that helps some.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.