package com.landtool.lanbase.modules.api.utils;
|
|
|
import java.io.File;
|
|
/**
|
* @Description: 删除文件
|
* @author ykm
|
* @param
|
* @return
|
* @date 2018/4/19 16:42
|
*/
|
public class DeleteFileUtils {
|
|
public static boolean deletefile(String filePath) {
|
File file = new File(filePath);
|
if(file.exists() && file.isFile()) {
|
if(file.delete()) {
|
System.out.println("删除文件成功");
|
return true;
|
}
|
else {
|
System.out.println("删除文件失败");
|
return false;
|
}
|
}
|
else {
|
System.out.println(filePath + "此路径不存在文件");
|
return false;
|
}
|
}
|
|
public static boolean deleteDiretory(String dirPath) {
|
if(!dirPath.endsWith(File.separator)) {
|
dirPath = dirPath + File.separator;
|
File file = new File(dirPath);
|
boolean flag = false;
|
if(file.exists() && file.isDirectory()) {
|
File[] files = file.listFiles();
|
for (int i=0;i<files.length;i++) {
|
if(files[i].isFile()) {
|
flag = DeleteFileUtils.deletefile(files[i].getAbsolutePath());
|
if(!flag) break;
|
}
|
else if(files[i].isDirectory()) {
|
flag = DeleteFileUtils.deleteDiretory(files[i].getAbsolutePath());
|
if(!flag) break;
|
}
|
}
|
}
|
if(!flag) {
|
System.out.println("删除文件夹失败");
|
return false;
|
}
|
if(file.delete()) {
|
System.out.println("删除文件夹成功");
|
return true;
|
}
|
else {
|
System.out.println("删除文件及失败");
|
return false;
|
}
|
}
|
else {
|
System.out.println("文件夹不存在");
|
return false;
|
}
|
}
|
|
public static boolean deletefileUtils(String filepath) {
|
File file = new File(filepath);
|
if(file.exists()) {
|
if(file.isFile()) {
|
return DeleteFileUtils.deletefile(filepath);
|
}
|
else if (file.isDirectory()) {
|
return DeleteFileUtils.deleteDiretory(filepath);
|
}
|
else {
|
System.out.println("无法判断类型");
|
return false;
|
}
|
}
|
else {
|
System.out.println("文件不存在");
|
return false;
|
}
|
}
|
}
|