一個File對象
import java
public class DeleteAll {
//刪除文件夾下所有內容
public void delAll(File f) throws IOException {
if(!f
throw new IOException(
boolean rslt=true;//保存中間結果
if(!(rslt=f
//若文件夾非空
File subs[] = f
for (int i =
if (subs[i].isDirectory())
delAll(subs[i]);//遞歸刪除子文件夾內容
rslt = subs[i].delete();//刪除子文件夾本身
}
rslt = f.delete();//刪除此文件夾本身
}
if(!rslt)
throw new IOException("無法刪除:"+f.getName());
return;
}
public static void main(String[] args) {
DeleteAll da= new DeleteAll();
try {
da.delAll(new File("someDir"));
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26727.html