java删除文件及其目录

1.删除指定文件路径

 public @ResponseBody
String deleteFiles(HttpServletRequest request) {
log.info(this.getClass().getSimpleName() + "."
+ Thread.currentThread().getStackTrace()[1].getMethodName()
+ "()------start");
// 身份校验
String random = request.getParameter("random");
String checkCode = request.getParameter("checkCode");
String reCheckCode=MD5Util.encrypt("pplovemm"+random.substring(random.length()-6));
String result = "";
if(reCheckCode.equals(checkCode)){
// 指定路径文件
String newFilePath="E:\\video\\2017-07-07\\";
log.info("Delete file or path:"+newFilePath);
System.out.println("Delete file or path:"+newFilePath);
if (newFilePath.startsWith(newFilePath) && clearFiles(newFilePath)) {
result = "success";
} else {
result = "fail";
}
log.info(this.getClass().getSimpleName() + "."
+ Thread.currentThread().getStackTrace()[1].getMethodName()
+ "()--------end");
return result;
}else {
result = "fail";
log.info(this.getClass().getSimpleName() + "."
+ Thread.currentThread().getStackTrace()[1].getMethodName()
+ "()-身份校验失败--end");
return result;
}

2.clearFiles方法:

     // 删除文件和目录
private static boolean clearFiles(String workspaceRootPath) {
File file = new File(workspaceRootPath);
if (file.exists()) {
deleteFile(file);
}
// resources 文件夹被删除后需新建
if (!file.exists() && workspaceRootPath.endsWith("resources")) {
return file.mkdir();
}else if(!file.exists()){
return true;
}
return false;
}
上一篇:【改造Linux命令之rm - 删除文件或目录-】


下一篇:Debian 7 安装 Docker