Traversing the Files and Directories Under a Directory

This example implements methods that recursively visits all files and directories under a directory.
// 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]));
        }
    }
}

// Process only directories under dir
public static void visitAllDirs(File dir) {
    if (dir.isDirectory()) {
        process(dir);

        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            visitAllDirs(new File(dir, children[i]));
        }
    }
}

// Process only files under dir
public static void visitAllFiles(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            visitAllFiles(new File(dir, children[i]));
        }
    } else {
        process(dir);
    }
}

Comments

22 Jan 2010 - 11:09am by shyam (not verified)

wonderful it was help!

3 Feb 2010 - 4:30am by Anonymous (not verified)

Thanks, it helped a lot :)

12 Feb 2010 - 6:50am by Anonymous (not verified)

thank you. It does help a lot!

3 May 2010 - 1:27pm by Anonymous (not verified)

Thank you, it really helped :) Very simple, although not that obvious snippets for a starter like me :)

3 May 2010 - 1:27pm by Anonymous (not verified)

Thank you, it really helped :) Very simple, although not that obvious snippets for a starter like me :)

13 May 2010 - 4:11pm by David Liou Chen (not verified)

These are great examples... very helpful.

8 Jun 2010 - 11:40am by Anonymous (not verified)

Very Helpful....

10 Jun 2010 - 9:29am by Anonymous (not verified)

its good:)

19 Sep 2010 - 8:50am by Tuong Minh (not verified)

Thank you, it's very helpful!

26 Nov 2010 - 7:07am by Anonymous (not verified)

cool...

23 Dec 2010 - 2:37pm by Anonymous (not verified)

so, Totally new...process(Dir) doesnt...isnt defined, is that something i define in Main?

5 Apr 2011 - 2:04am by Ngoc Chan (not verified)

//Or by using a stack:

public static void visitAllFilesByStack(File dir){
Stack files = new Stack();
files.push(dir);
while (!files.empty()){
File d = files.pop();
if (d.isDirectory()){
String[] children = d.list();
for (int i=0; i

15 Apr 2011 - 8:40am by Anonymous (not verified)

Thank you. It was perfect for me.

19 Apr 2011 - 5:37am by vimal kumar (not verified)

thank you very much and i am sharing some code which is very helpful to all to find path of a directory or a file and dividing that path into tokens and insert that path data into database and copy that path file into some folder.
This example like u have folders:/home/sample/hi/hi1/hi2/hi3/hi4/hi.java,/home/sample/hi/hi1/hi_1/hi_2/hi_3/hello.java
import java.io.*;
import java.util.*;
import java.sql.*;

class dirlist
{
String filepath="";
Connection con=null;
Statement st1=null;
public void connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","root");
st1=con.createStatement();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
public void visitAllDirs(String filepath1)
{
try
{
String filename="";
String filepath=filepath1;
File test;
File dir=new File(filepath);
if (dir.isDirectory())
{
String[] children = dir.list();
String[] child=new String[children.length];
if(children.length!=0)
{
for (int i=0; i 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch(Exception e3)
{
System.out.println(e3);
}
}
public static void main(String[] args)
{
String filepath1="/home/sample/hi/";
dirlist dlist=new dirlist();
dlist.connect();
dlist.visitAllDirs(filepath1);
}
}

19 Apr 2011 - 5:46am by Anonymous

previous code vistAllDirectories() method complete code
if (dir.isDirectory())
{
String[] children = dir.list();
String[] child=new String[children.length];
if(children.length!=0)
{
for (int i=0; i

28 Apr 2011 - 12:59am by msakr (not verified)

In response to Anonymous @ 23 Dec 2010 - 2:37pm

process is the method you should create to actually process each matched file/dir. A simple implementation would go something like:

public static void process(String dir) {
System.out.println("Matched: " + dir);
}

25 Jun 2011 - 5:35am by Anonymous (not verified)

Thank u so much..

14 Nov 2011 - 2:45pm by Anonymous (not verified)

alert("fffffff");

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.