新增:file工具类,文件夹获取方法新增

This commit is contained in:
2026-03-25 16:57:51 +08:00
parent 99a82a21b8
commit 3ae446d9a5

View File

@@ -818,6 +818,57 @@ public class FilesUtil {
}
}
/**
* 获取基础路径下所有一级子文件夹的绝对路径
*
* @param basePath 基础路径,如 /hpc/123/uuid/ 或 D:/hpc/123/uuid/
* @return 所有子文件夹的绝对路径列表
*/
public static List<String> getAllSubFolderAbsolutePaths(String basePath) {
List<String> folderPaths = new ArrayList<>();
// 1. 构建基础路径文件对象
File baseDir = new File(basePath);
// 2. 校验路径是否存在 & 是否是文件夹
if (!baseDir.exists()) {
throw new IllegalArgumentException("基础路径不存在:" + basePath);
}
if (!baseDir.isDirectory()) {
throw new IllegalArgumentException("基础路径不是文件夹:" + basePath);
}
// 3. 获取路径下所有文件/文件夹
File[] files = baseDir.listFiles();
if (files == null || files.length == 0) {
return folderPaths; // 空文件夹,返回空列表
}
// 4. 筛选出【文件夹】,并获取绝对路径
for (File file : files) {
if (file.isDirectory()) {
folderPaths.add(file.getAbsolutePath());
}
}
return folderPaths;
}
/**
* 从路径中获取最后一层目录名(兼容所有系统、兼容末尾带 /
* 示例:
* /hpc/123/uuid -> uuid
* /hpc/123/uuid/ -> uuidd
*/
public static String getLastDirectoryName(String path) {
if (path == null || path.isBlank()) {
return null;
}
Path filePath = Paths.get(path).normalize();
Path fileName = filePath.getFileName();
return fileName == null ? null : fileName.toString();
}
}