Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-04-2008, 02:52 PM   PM User | #1
bbd
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
bbd is an unknown quantity at this point
Question Traversing Folders

Hey,

Basically I need to get a list of File pointers to ever file in a directory, including all subdirectories.

Currently I do this, but it only gets the files in that directory.

File directory = new File("c:\\MyDir");
File[] files = directory.listFiles();

I need everything in c:\MyDir\AnyOtherSubDir
Is there some already created easy code that I can use?

Thanks!

BBD
bbd is offline   Reply With Quote
Old 09-04-2008, 03:41 PM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
here is a simple example

PHP Code:
import java.io.*;

public class 
RecursiveReader {
    
/*
      * Constructor for RecursiveReader
      */
    
public RecursiveReader(){
        
// get handler to current directory
        
File currDir = new File(System.getProperty("user.dir"));
        
        
this.recursiveDirReader(currDir);
    } 
//-- ends class constructor
    
    
private void recursiveDirReader(final File handler) {
        if (
handler.isDirectory()){
            
File [] files handler.listFiles();
            
System.out.println("Now Reading: "handler.getAbsolutePath());
            for(
File f files) {
                if (
f.isFile()){
                    
// do something with the file.
                    
System.out.println("File: '" f.getName());
                } 
//-- end if block
                
else if(f.isDirectory()){
                    
// if directory...call the method for recursion
                    
System.out.println("Now Reading: "f.getAbsolutePath());
                    
recursiveDirReader(f);
                } 
//-- ends else if block
            
//-- ends foreach block
        
//-- end if block
    
//-- ends 
    
    
public static void main(String[] args) {
        new 
RecursiveReader();
    } 
//-- ends class method main
    
//-- ends class definition 
Cheers
~E
ess is offline   Reply With Quote
Reply

Bookmarks

Tags
java files directory list

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:16 PM.


Advertisement
Log in to turn off these ads.