zoukankan      html  css  js  c++  java
  • 删除某个文件夹下的所有文件夹和文件

    删除某个文件夹下的所有文件夹和文件
     1     /**
     2      * 删除某个文件夹下的所有文件夹和文件
     3      *
     4      * @param delpath 文件路径
     5      * @return
     6      * @throws Exception
     7      */
     8     public static boolean deletefile(String delpath) throws Exception {
     9            try {
    10 
    11                File file = new File(delpath);
    12                // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
    13                if (!file.isDirectory()) {
    14                    file.delete();
    15                } else if (file.isDirectory()) {
    16                    String[] filelist = file.list();
    17                    for (int i = 0; i < filelist.length; i++) {
    18                        File delfile = new File(delpath + "\" + filelist[i]);
    19                        if (!delfile.isDirectory()) {
    20                            delfile.delete();
    21                            System.out.println(delfile.getAbsolutePath() + "删除文件成功");
    22                        } else if (delfile.isDirectory()) {
    23                            deletefile(delpath + "\" + filelist[i]);
    24                            System.out.println(file + "ssss");
    25                        }
    26                    }
    27                 //选择不删除自身文件夹
    28                    if (!file.toString().equals("d:\test")) {
    29                        System.out.println(file.toString() + "lllllll");
    30                        file.delete();
    31                    }
    32                }
    33 
    34            } catch (FileNotFoundException e) {
    35                System.out.println("deletefile() Exception:" + e.getMessage());
    36            }
    37            return true;
    38        }

     文件夹不存在,则创建文件夹

        /**
         * 删除某个文件夹下的所有文件夹和文件
         *
         * @param delpath 文件路径
         * @return
         * @throws Exception
         */
        public static boolean deletefile(String delpath) throws Exception {
               try {
    
                   File file = new File(delpath);
                //如果文件夹不存在,创建文件夹
                if(!file.exists()) {
                    file.mkdir();
                } else {
                    // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true
                    if (!file.isDirectory()) {
                        file.delete();
                    } else if (file.isDirectory()) {
                        String[] filelist = file.list();
                        for (int i = 0; i < filelist.length; i++) {
                            File delfile = new File(delpath + "\" + filelist[i]);
                            if (!delfile.isDirectory()) {
                                delfile.delete();
                                System.out.println(delfile.getAbsolutePath() + "删除文件成功");
                            } else if (delfile.isDirectory()) {
                                deletefile(delpath + "\" + filelist[i]);
                            }
                        }
                        //选择不删除自身文件夹
                        if (!file.toString().equals("d:\test")) {
                            file.delete();
                        }
                    }
                }
               } catch (FileNotFoundException e) {
                   System.out.println("deletefile() Exception:" + e.getMessage());
               }
               return true;
           }
  • 相关阅读:
    基础字段及选项2(11)
    模型层及ORM介绍(9)
    Luogu [P3367] 模板 并查集
    Luogu [P1958] 上学路线_NOI导刊2009普及(6)
    Luogu [P3951] 小凯的疑惑
    Luogu [P2708] 硬币翻转
    Luogu [P1334] 瑞瑞的木板(手写堆)
    一步步学习如何建立自己的个性博客~~
    Android初学者—listView用法
    SQLite命令—对表插入和修改等操作
  • 原文地址:https://www.cnblogs.com/chendezhen/p/14627688.html
Copyright © 2011-2022 走看看