import java.io.File;
import java.io.IOException;
public class Deldirectory {
/**
* @param args
*/
public static void del(String filepath) throws IOException {
File f = new File(filepath);// 定義文件路徑
if (f.exists() && f.isDirectory()) {// 判斷是文件還是目錄
if (f.listFiles().length == 0) {// 若目錄下沒有文件則直接刪除
f.delete();
} else {// 若有則把文件放進數組,并判斷是否有下級目錄
File delFile[] = f.listFiles();
int i = f.listFiles().length;
for (int j = 0; j < i; j++) {
if (delFile[j].isDirectory()) {
del(delFile[j].getAbsolutePath());// 遞歸調用del方法并取得子目錄路徑
}
delFile[j].delete();// 刪除文件
}
}
del(filepath);// 遞歸調用
}
}
public static void main(String[] args) {
try {
Deldirectory.del("d:\\sellindex");
} catch (IOException e) {
e.printStackTrace();
}
}
}
posted on 2007-12-19 11:02
方濤升 閱讀(302)
評論(0) 編輯 收藏 所屬分類:
j2ee