java中如何删除本地文件夹以及文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/system_err/article/details/81771715
File file = new File("C:/zc.xls");
if (file.isFile() && file.exists()) {
file.delete();
}

如果文件下面有东西的话,要一层层的删

public void delTempChild(File file){
if (file.isDirectory()) {
      String[] children = file.list();//获取文件夹下所有子文件夹
    //递归删除目录中的子目录下
      for (int i=0; i<children.length; i++) {
      delTempChild(new File(file, children[i])); 
      }
  }
  // 目录空了,进行删除
  file.delete();
}

猜你喜欢

转载自blog.csdn.net/system_err/article/details/81771715
今日推荐