import java.io.File;
public class IterateFilesExample2 {
public static void scanDir(String path) throws Exception {
File currentDir = new File(path);
if (currentDir.isFile()) {
System.out.println(currentDir.getAbsoluteFile());
} else {
System.out.println(currentDir.getAbsoluteFile());
}
if (currentDir.isDirectory()) {
String[] items = currentDir.list();
for (String name : items) {
scanDir(path + "/" + name);
}
}
}
public static void main(String[] args) throws Exception {
scanDir("d:\\usr");
}
}
import java.io.File;
import java.util.ArrayList;
public class IterateFilesExample3 {
public static ArrayList<String> fileArr = new ArrayList<String>();
public static void scanDir(String path) throws Exception {
File currentDir = new File(path);
if (currentDir.isFile()) {
fileArr.add(currentDir.getAbsolutePath());
System.out.println(currentDir.getAbsolutePath());
} else {
System.out.println(currentDir.getAbsolutePath());
}
if (currentDir.isDirectory()) {
String[] items = currentDir.list();
for (String name : items) {
scanDir(path + "/" + name);
}
}
}
public static void main(String[] args) throws Exception {
scanDir("d:\\usr");
System.out.println(fileArr.toString());
}
}