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
  • 相关阅读:
    Tomcat笔记
    HAProxy简介
    Ansible简介与实验
    DHCP&PXE&Cobbler简单实现
    Keepalived实现LVS-DR集群高可用
    Web of Science爬虫(WOS、SCI):风车WOS下载器
    梦境时分,记录梦境
    基层码农谈领导力
    企业部署 Kubernetes 的终极目标是什么
    Scrum 敏捷实践中的三大角色
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/7262786.html
Copyright © 2011-2022 走看看