Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to list all directories in a webapp  RSS feed

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I want to list all the directories in a web application. Is there an easy way to get an array of all the directories?

In the java.io.File api there is a method called list(), but this method only returns an array of files and directories in the directory denoted by this abstract pathname...

How can I easy get a list of all the other sub directories?
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I think I found a solution:

http://javaalmanac.com/egs/java.io/TraverseTree.html
 
preddy Narra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have a doubt.in your code you have mentioned process(dir).what is meant by?..
and give me the code which read all files in a given directory..

suppose i have a directory like d:\core\
--in "core" directory lot of directories are there..
--and i want to locate each and every directory and list out all the files in each directory..

please help me out..

from PC..
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The process method is where you list all dirs or files in one directory:

http://javaalmanac.com/egs/java.io/GetFiles.html

Example:

public java.io.File[] process( java.io.File dir ){

File[] files = dir.listFiles();
FileFilter fileFilter = new FileFilter() {

public boolean accept(File file) {
return file.isDirectory();
}
};

files = dir.listFiles(fileFilter);

if (files == null) {
return null;
// Either dir does not exist or is not a directory
}
else {
return files;
}

To list all dirs and files you can use this method:

http://javaalmanac.com/egs/java.io/TraverseTree.html

// Process all files and directories under dir
public static void visitAllDirsAndFiles(File dir) {
process(dir);

if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}
[ July 10, 2006: Message edited by: Jeppe Fjord ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!