一、对于文件夹的创建和删除工具类
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @Description :
* @Date : 2023/6/3
* @Author :
*/
@Slf4j
public class FileUtil {
/**
* 创建文件夹
*/
public static void createFold(String path) {
File f = new File(path);
f.setWritable(true, false); //设置写权限,windows下不用此语句
f.mkdirs();
}
/**
* 获取obs的url的对应的objectKey
*
* @param url 例如https://slw-base-video-test.obs.cn-north-4.myhuaweicloud.com/ThirdAlarm/video/email/94446544-a372-4181-b03c-e719a6d5275a.tar
* @param endPoint 例如obs.cn-north-4.myhuaweicloud.com
* @return
*/
public static String getFilePath(String url, String endPoint) {
if (StrUtil.isBlank(url) || StrUtil.isBlank(endPoint)) {
return url;
}
int i = url.indexOf(endPoint);
int size = i + endPoint.length();
String sub = StrUtil.sub(url, size, url.length());
int i1 = sub.indexOf("/");
String path = StrUtil.sub(sub, i1 + 1, sub.length());
return path;
}
/**
* @param url 获取obs返回的url的文件后缀
* https://slw-base-video-test.obs.cn-north-4.myhuaweicloud.com/ThirdAlarm/pic/photo/a1c9d9c2bac04e73a41c39b4a08d1a3d.xlsx
* @return
*/
public static String getSuffix(String url, String endPoint) {
String filePath = getFilePath(url, endPoint);
int i = filePath.lastIndexOf(".");
String suffix = StrUtil.sub(filePath, i + 1, filePath.length());
return suffix;
}
/**
* @param url 获取obs返回的url的文件名
* @return
*/
public static String getFileName(String url, String endPoint) {
String filePath = getFilePath(url, endPoint);
int i = filePath.lastIndexOf("/");
String fileName = StrUtil.sub(filePath, i + 1, filePath.length());
return fileName;
}
/**
* 删除单个文件
*
* @param fileName
* @return
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径只有单个文件
if (file.exists() && file.isFile()) {
if (file.delete()) {
log.info("删除文件" + fileName + "成功!");
return true;
} else {
log.info("删除文件" + fileName + "失败!");
return false;
}
} else {
log.info(fileName + "不存在!");
return false;
}
}
/**
* 删除目录下所有文件,包括本文件夹
*
* @param directory
* @return
*/
public static boolean deleteFiles(File directory) {
boolean flag = true;
if ((!directory.exists()) || (!directory.isDirectory())) {
log.info("删除文件夹失败不存在!");
return false;
}
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteFiles(file); // 递归删除子目录中的文件
} else {
try {
Path path = file.toPath();
Files.delete(path); // 删除文件
log.info("Deleted: " + file.getAbsolutePath());
} catch (IOException e) {
log.error("Error deleting file: " + file.getAbsolutePath() + " - " + e.getMessage());
flag = false;
}
}
}
}
try {
Files.delete(directory.toPath()); // 删除目录
log.info("Deleted directory: " + directory.getAbsolutePath());
} catch (IOException e) {
log.error("Error deleting directory: " + directory.getAbsolutePath() + " - " + e.getMessage());
flag = false;
}
return flag;
}
/**
* 删除目录下所有文件,不删除本文件夹
*
* @param filepath
* @return
*/
public static boolean deleteAllFile(String filepath) {
boolean flag = false;
File file = new File(filepath);
if (!file.exists() || !file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (filepath.endsWith(File.separator)) {
temp = new File(filepath + tempList[i]);
} else {
temp = new File(filepath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
deleteAllFile(temp.getPath()); // 先删除文件夹里面的文件
temp.delete(); // 再删除空文件夹
flag = true;
}
}
return flag;
}
}
标签:return,String,删除,url,File,文件夹,file,directory,问价
From: https://www.cnblogs.com/cgy1995/p/17560147.html