天天看點

java 删除檔案夾_java 删除檔案夾2種實作

public static void deleteFolder(String folder){

File f = new File(folder);

if(!f.exists()){

return;

}

if(f.isFile()){

System.out.println("删除檔案"+f.getPath());

System.out.println("删除檔案狀态"+f.delete());

return;

}

if(f.isDirectory()){

File[] _f = f.listFiles();

for(File __f:_f){

if(__f.isFile()) {

System.out.println("删除檔案"+__f.getPath());

System.out.println("删除檔案狀态"+__f.delete());

}

if(__f.isDirectory()){

deleteFolder(__f.getPath());

}

}

File[] _f2 = f.listFiles();

if(_f2.length == 0){

System.out.println("删除目錄"+f.getPath());

System.out.println("删除目錄"+f.delete());

}

}

}

public static void deleteFolder(String folder,String root){

File f = new File(folder);

if(!f.exists()){

return;

}

if(f.isFile()){

System.out.println("删除檔案"+f.getPath());

System.out.println("删除檔案狀态"+f.delete());

return;

}

if(f.isDirectory()){

File[] _f = f.listFiles();

for(File __f:_f){

if(__f.isFile()) {

System.out.println("删除檔案"+__f.getPath());

System.out.println("删除檔案狀态"+__f.delete());

}

if(__f.isDirectory()){

deleteFolder(__f.getPath(),root);

}

}

File[] _f2 = f.listFiles();

if(_f2.length == 0 && !root.equals(f.getPath())){

System.out.println("删除目錄"+f.getPath());

System.out.println("删除目錄"+f.delete());

}

}

}