递归算法是什么?
作者:网络转载 发布时间:[ 2013/1/10 9:50:23 ] 推荐标签:
程序执行流程如下:

③列出某个目录下所有的子目录和文件
求解代码:
[html] view plaincopyprint?
/*
* time:2012.12.2
* author:王金宇
* description:列出某个目录下所有的子目录和文件
*/
public class ListDir {
static void getDir(String strPath) throws Exception {
try {
File f = new File(strPath);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (int j = 0; j < fList.length; j++) {
if (fList[j].isDirectory()) {
System.out.println(fList[j].getPath());
getDir(fList[j].getPath()); // 在getDir函数里面又调用了getDir函数本身
}
}
for (int j = 0; j < fList.length; j++) {
if (fList[j].isFile()) {
System.out.println(fList[j].getPath());
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
public static void main(String[] args) {
String strPath = "E:";
System.out.println(strPath);
try {
getDir(strPath);
} catch (Exception e) {
}
}
}
/*
* time:2012.12.2
* author:王金宇
* description:列出某个目录下所有的子目录和文件
*/
public class ListDir {
static void getDir(String strPath) throws Exception {
try {
File f = new File(strPath);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (int j = 0; j < fList.length; j++) {
if (fList[j].isDirectory()) {
System.out.println(fList[j].getPath());
getDir(fList[j].getPath()); // 在getDir函数里面又调用了getDir函数本身
}
}
for (int j = 0; j < fList.length; j++) {
if (fList[j].isFile()) {
System.out.println(fList[j].getPath());
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
public static void main(String[] args) {
String strPath = "E:";
System.out.println(strPath);
try {
getDir(strPath);
} catch (Exception e) {
}
}
}

sales@spasvo.com