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);
}
}
wonderful it was help!
Thanks, it helped a lot :)
thank you. It does help a lot!
Thank you, it really helped :) Very simple, although not that obvious snippets for a starter like me :)
Thank you, it really helped :) Very simple, although not that obvious snippets for a starter like me :)
These are great examples... very helpful.
Very Helpful....
its good:)
Thank you, it's very helpful!
cool...
so, Totally new...process(Dir) doesnt...isnt defined, is that something i define in Main?
//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
Thank you. It was perfect for me.
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);
}
}
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
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);
}
Thank u so much..
alert("fffffff");