• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to list all directories in a webapp

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I think I found a solution:

http://javaalmanac.com/egs/java.io/TraverseTree.html
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic