zoukankan      html  css  js  c++  java
  • java删除文件及其目录

    1.删除指定文件路径

     1 public @ResponseBody
     2     String deleteFiles(HttpServletRequest request) {
     3         log.info(this.getClass().getSimpleName() + "."
     4                 + Thread.currentThread().getStackTrace()[1].getMethodName()
     5                 + "()------start");
     6         // 身份校验
     7         String random = request.getParameter("random");
     8         String checkCode = request.getParameter("checkCode");
     9         String reCheckCode=MD5Util.encrypt("pplovemm"+random.substring(random.length()-6));
    10         String result = "";
    11         if(reCheckCode.equals(checkCode)){ 
    12            // 指定路径文件
    13             String newFilePath="E:\video\2017-07-07\";
    14             log.info("Delete file or path:"+newFilePath);
    15             System.out.println("Delete file or path:"+newFilePath);
    16             if (newFilePath.startsWith(newFilePath) && clearFiles(newFilePath)) {
    17                 result = "success";
    18             } else {
    19                 result = "fail";
    20             }
    21             log.info(this.getClass().getSimpleName() + "."
    22                     + Thread.currentThread().getStackTrace()[1].getMethodName()
    23                     + "()--------end");
    24             return result;
    25         }else {
    26             result = "fail";
    27             log.info(this.getClass().getSimpleName() + "."
    28                     + Thread.currentThread().getStackTrace()[1].getMethodName()
    29                     + "()-身份校验失败--end");            
    30             return result;
    31         }        
    View Code

    2.clearFiles方法:

     1     // 删除文件和目录
     2     private static boolean clearFiles(String workspaceRootPath) {
     3         File file = new File(workspaceRootPath);
     4         if (file.exists()) {
     5             deleteFile(file);
     6         }
     7         // resources 文件夹被删除后需新建
     8         if (!file.exists() && workspaceRootPath.endsWith("resources")) {
     9             return file.mkdir();
    10         }else if(!file.exists()){
    11             return true;
    12         }
    13         return false;
    14     }
    View Code
  • 相关阅读:
    50种方法优化SQL Server
    VS2015在Windows 10 下面安装经验
    python中yield的用法详解——最简单,最清晰的解释
    Mac安装Allure
    Python之pymysql数据库操作
    Python操作Excel神器-openpyxl之写入
    Python OS 模块处理路径
    Python操作Excel神器-openpyxl之读取
    Python中的if __name__ == '__main__'
    Python元组常用方法及汇总
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/7262786.html
Copyright © 2011-2022 走看看