zoukankan      html  css  js  c++  java
  • 通过io流删除文件夹(包括文件夹下的所有文件)

    io流在删除文件夹的时候,该文件夹里面必须没有文件,如果存在文件,则无法删除(window系统也是如此,window系统是通过递归的方式来先删除文件夹里面的文件,再删除该文件夹的方式来删除的!),所有必须通过程序先删除该文件夹里面的文件,再删除这个文件夹!

    //删除指定文件夹下所有文件
    //param path 文件夹完整绝对路径
    public static boolean delAllFile(String path) {
      boolean flag = false;
      File file = new File(path);
      if (!file.exists()) {
        return flag;
      }
      if (!file.isDirectory()) {
        return flag;
      }
      String[] tempList = file.list();
      File temp = null;
      for (int i = 0; i < tempList.length; i++) {
        if (path.endsWith(File.separator)) {
          temp = new File(path + tempList[i]);
        } else {
          temp = new File(path + File.separator + tempList[i]);
        }
        if (temp.isFile()) {
          temp.delete();
        }
        if (temp.isDirectory()) {
          delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
          delFolder(path + "/" + tempList[i]);//再删除空文件夹
          flag = true;
        }
      }
      return flag;
    }
    //删除文件夹
    //param folderPath 文件夹完整绝对路径
    public static void delFolder(String folderPath) {
      try {
        delAllFile(folderPath); //删除完里面所有内容
        String filePath = folderPath;
        filePath = filePath.toString();
        java.io.File myFilePath = new java.io.File(filePath);
        myFilePath.delete(); //删除空文件夹
      } catch (Exception e) {
        e.printStackTrace(); 
      }
    }
    //调用方法测试
    public void test(){   String path = "C:/xx1/xx2";   delFolder(path);//会把xx2下的所有文件包括xx2这个文件夹都删了 }

      

  • 相关阅读:
    cocos3 单击
    cocos3 帧动画
    cocos3 动作和帧动画
    cocos3 场景切换特效
    cocos3 场景切换
    cocos3 error C2440
    c++ 匿名函数
    【leetcode】生成每种字符都是奇数个的字符串
    【leetcode】山羊拉丁文
    【leetcode】字符串的最大公因子
  • 原文地址:https://www.cnblogs.com/xuehuashanghe/p/9530939.html
Copyright © 2011-2022 走看看