Java刪除文件夾和子文件方法

admin 系統(tǒng)教程評論1,088字數(shù) 601閱讀模式

Java刪除文件夾和子文件方法

public class Test { 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

public static void main(String[] args){ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

File file = new File("D:\\a"); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

delAllFile(file); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

} 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

/** 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

* 刪除文件或文件夾 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

* @param directory 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

*/ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

public static void delAllFile(File directory){ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

if (!directory.isDirectory()){ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

directory.delete(); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

} else{ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

File [] files = directory.listFiles(); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

// 空文件夾 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

if (files.length == 0){ 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

directory.delete(); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

System.out.println("刪除" + directory.getAbsolutePath()); 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

return; 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

} 文章源自網吧系統(tǒng)維護-http://www.strong-digital.cn/4013.html

// 刪除子文件夾和子文件

for (File file : files){

if (file.isDirectory()){

delAllFile(file);

} else {

file.delete();

System.out.println("刪除" + file.getAbsolutePath());

}

}

// 刪除文件夾本身

directory.delete();

System.out.println("刪除" + directory.getAbsolutePath());

}

}

版權聲明:文章圖片資源來源于網絡,如有侵權,請留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2019年9月8日 15:14:36
  • 轉載請務必保留本文鏈接:http://www.strong-digital.cn/4013.html
匿名

發(fā)表評論

匿名網友 填寫信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: