//用遞歸算法列出某個目錄下的所有子目錄和文件
import java.io.*;
class DiGuiGetDir
{
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函數(shù)里面又調(diào)用了getDir函數(shù)本身
????? }
???? }
???? 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="d:\\Download";
?? System.out.println(strPath);
?? try
?? {
??? getDir(strPath);
?? }
?? catch(Exception e)
?? {
?? }
}
}
posted on 2007-05-19 10:45
jadmin 閱讀(115)
評論(0) 編輯 收藏