• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How to list all directories in a webapp  RSS feed

 
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
 
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 ]
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!