import java.io.*;
/**
*
* 该类实现列出某个目录下的所有目录和文件的功能.
* 给定路径如:strPath = e:\\Flash
* 在命令行输入 java FileTree e:\\Flash
*
* @author Administrator
*/
public class FileTree {
public static void getDirAndFiles(String strPath) throws Exception {
File f = new File(strPath);
if(f.isDirectory()) {
File[] fList = f.listFiles();
/**
* 列出当前目录下的目录和文件
*/
for(int i = 0; i < fList.length; i ++) {
if(f.isDirectory()) {
System.out.println(fList[i].getPath());//也可把目录加入集合
getDirAndFiles(fList[i].getPath()); //递归调用
}
if(fList[i].isFile()) {
System.out.println(fList[i].getPath()); //也可把文件加入集合中
}
}
}
}
public static void main(String[] args) {
String strPath = args[0];
try {
getDirAndFiles(strPath);
}catch (Exception e) {
e.printStackTrace();
}
}
}
列出某个目录下的所有目录和文件
最新推荐文章于 2023-10-06 20:56:39 发布