手艺人赵鹏

技术也是一门手艺

java删除文件包含子文件

//文件删除测试



import java.io.*;



public class Delete {



 public static void main(String[] args) throws IOException {

  

  Delete.delete("F:b");



  System.out.println("ok");

 }



 

 

 //文件名或者文件删除(可以删除包含的子文件和文件夹)

 public static boolean delete(String s) throws IOException {

  File file = new File(s);

  if(!file.delete()) {

   String[] f = file.list();//列出当前文件夹中的所以文件包括文件夹

&nbsp;&nbsp;&nbsp;for(int i = 0; i < f.length; i++) {

&nbsp;&nbsp;&nbsp;&nbsp;delete(file.getAbsolutePath() + "" + f[i]);//对子文件夹进行处理

&nbsp;&nbsp;&nbsp;&nbsp;return &nbsp;delete(file.getAbsolutePath());//返回上级目录

&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;}

&nbsp;&nbsp;return true;

&nbsp;}



&nbsp;



}

Leave a Reply