mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -5,6 +5,7 @@ import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.io.file.FileSystemUtil;
|
||||
import cn.hutool.core.io.file.PathUtil;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
@@ -172,7 +173,7 @@ public class ZipUtil {
|
||||
* @throws UtilException IO异常
|
||||
*/
|
||||
public static File zip(final File srcFile, final Charset charset) throws UtilException {
|
||||
final File zipFile = FileUtil.file(srcFile.getParentFile(), FileUtil.mainName(srcFile) + ".zip");
|
||||
final File zipFile = FileUtil.file(srcFile.getParentFile(), FileNameUtil.mainName(srcFile) + ".zip");
|
||||
zip(zipFile, charset, false, srcFile);
|
||||
return zipFile;
|
||||
}
|
||||
@@ -468,7 +469,7 @@ public class ZipUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static File unzip(final File zipFile, final Charset charset) throws UtilException {
|
||||
final File destDir = FileUtil.file(zipFile.getParentFile(), FileUtil.mainName(zipFile));
|
||||
final File destDir = FileUtil.file(zipFile.getParentFile(), FileNameUtil.mainName(zipFile));
|
||||
return unzip(zipFile, destDir, charset);
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.codec.HexUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -139,10 +140,10 @@ public class FileTypeUtil {
|
||||
String typeName = getType(in,isExact);
|
||||
if (null == typeName) {
|
||||
// 未成功识别类型,扩展名辅助识别
|
||||
typeName = FileUtil.extName(filename);
|
||||
typeName = FileNameUtil.extName(filename);
|
||||
} else if ("zip".equals(typeName)) {
|
||||
// zip可能为docx、xlsx、pptx、jar、war、ofd等格式,扩展名辅助判断
|
||||
final String extName = FileUtil.extName(filename);
|
||||
final String extName = FileNameUtil.extName(filename);
|
||||
if ("docx".equalsIgnoreCase(extName)) {
|
||||
typeName = "docx";
|
||||
} else if ("xlsx".equalsIgnoreCase(extName)) {
|
||||
@@ -160,7 +161,7 @@ public class FileTypeUtil {
|
||||
}
|
||||
} else if ("jar".equals(typeName)) {
|
||||
// wps编辑过的.xlsx文件与.jar的开头相同,通过扩展名判断
|
||||
final String extName = FileUtil.extName(filename);
|
||||
final String extName = FileNameUtil.extName(filename);
|
||||
if ("xlsx".equalsIgnoreCase(extName)) {
|
||||
typeName = "xlsx";
|
||||
} else if ("docx".equalsIgnoreCase(extName)) {
|
||||
|
@@ -660,11 +660,10 @@ public class FileUtil extends PathUtil {
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* @param fullFileOrDirPath 文件或者目录的路径
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static boolean del(final String fullFileOrDirPath) throws IORuntimeException {
|
||||
return del(file(fullFileOrDirPath));
|
||||
public static void del(final String fullFileOrDirPath) throws IORuntimeException {
|
||||
del(file(fullFileOrDirPath));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -672,42 +671,13 @@ public class FileUtil extends PathUtil {
|
||||
* 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹<br>
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* <p>
|
||||
* 从5.7.6开始,删除文件使用{@link Files#delete(Path)}代替 {@link File#delete()}<br>
|
||||
* 因为前者遇到文件被占用等原因时,抛出异常,而非返回false,异常会指明具体的失败原因。
|
||||
* </p>
|
||||
*
|
||||
* @param file 文件对象
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
* @see Files#delete(Path)
|
||||
*/
|
||||
public static boolean del(final File file) throws IORuntimeException {
|
||||
if (file == null || false == file.exists()) {
|
||||
// 如果文件不存在或已被删除,此处返回true表示删除成功
|
||||
return true;
|
||||
}
|
||||
|
||||
if (file.isDirectory()) {
|
||||
// 清空目录下所有文件和目录
|
||||
final boolean isOk = clean(file);
|
||||
if (false == isOk) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文件或清空后的目录
|
||||
final Path path = file.toPath();
|
||||
try {
|
||||
delFile(path);
|
||||
} catch (final DirectoryNotEmptyException e) {
|
||||
// 遍历清空目录没有成功,此时补充删除一次(可能存在部分软链)
|
||||
del(path);
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
|
||||
return true;
|
||||
public static void del(final File file) throws IORuntimeException {
|
||||
Assert.notNull(file, "File must be not null!");
|
||||
PathUtil.del(file.toPath());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -716,12 +686,10 @@ public class FileUtil extends PathUtil {
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* @param dirPath 文件夹路径
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static boolean clean(final String dirPath) throws IORuntimeException {
|
||||
return clean(file(dirPath));
|
||||
public static void clean(final String dirPath) throws IORuntimeException {
|
||||
clean(file(dirPath));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -730,52 +698,11 @@ public class FileUtil extends PathUtil {
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* @param directory 文件夹
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 3.0.6
|
||||
*/
|
||||
public static boolean clean(final File directory) throws IORuntimeException {
|
||||
if (directory == null || directory.exists() == false || false == directory.isDirectory()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final File[] files = directory.listFiles();
|
||||
if (null != files) {
|
||||
for (final File childFile : files) {
|
||||
if (false == del(childFile)) {
|
||||
// 删除一个出错则本次删除任务失败
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理空文件夹<br>
|
||||
* 此方法用于递归删除空的文件夹,不删除文件<br>
|
||||
* 如果传入的文件夹本身就是空的,删除这个文件夹
|
||||
*
|
||||
* @param directory 文件夹
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 4.5.5
|
||||
*/
|
||||
public static boolean cleanEmpty(final File directory) throws IORuntimeException {
|
||||
if (directory == null || false == directory.exists() || false == directory.isDirectory()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final File[] files = directory.listFiles();
|
||||
if (ArrayUtil.isEmpty(files)) {
|
||||
// 空文件夹则删除之
|
||||
return directory.delete();
|
||||
}
|
||||
|
||||
for (final File childFile : files) {
|
||||
cleanEmpty(childFile);
|
||||
}
|
||||
return true;
|
||||
public static void clean(final File directory) throws IORuntimeException {
|
||||
Assert.notNull(directory, "File must be not null!");
|
||||
PathUtil.clean(directory.toPath());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -978,7 +905,7 @@ public class FileUtil extends PathUtil {
|
||||
* </pre>
|
||||
*
|
||||
* @param src 源文件
|
||||
* @param target 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
|
||||
* @param target 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
|
||||
* @param isOverride 是否覆盖目标文件
|
||||
* @return 目标目录或文件
|
||||
* @throws IORuntimeException IO异常
|
||||
@@ -1013,9 +940,9 @@ public class FileUtil extends PathUtil {
|
||||
Assert.notNull(src, "Src file must be not null!");
|
||||
Assert.notNull(target, "target file must be not null!");
|
||||
return PathUtil.copyContent(
|
||||
src.toPath(),
|
||||
target.toPath(),
|
||||
isOverride ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{})
|
||||
src.toPath(),
|
||||
target.toPath(),
|
||||
isOverride ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING} : new CopyOption[]{})
|
||||
.toFile();
|
||||
}
|
||||
|
||||
@@ -1086,7 +1013,7 @@ public class FileUtil extends PathUtil {
|
||||
*/
|
||||
public static File rename(final File file, String newName, final boolean isRetainExt, final boolean isOverride) {
|
||||
if (isRetainExt) {
|
||||
final String extName = FileUtil.extName(file);
|
||||
final String extName = FileNameUtil.extName(file);
|
||||
if (StrUtil.isNotBlank(extName)) {
|
||||
newName = newName.concat(".").concat(extName);
|
||||
}
|
||||
@@ -1518,129 +1445,6 @@ public class FileUtil extends PathUtil {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------- name start
|
||||
|
||||
/**
|
||||
* 返回文件名
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 文件名
|
||||
* @see FileNameUtil#getName(File)
|
||||
* @since 4.1.13
|
||||
*/
|
||||
public static String getName(final File file) {
|
||||
return FileNameUtil.getName(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回文件名<br>
|
||||
* <pre>
|
||||
* "d:/test/aaa" 返回 "aaa"
|
||||
* "/test/aaa.jpg" 返回 "aaa.jpg"
|
||||
* </pre>
|
||||
*
|
||||
* @param filePath 文件
|
||||
* @return 文件名
|
||||
* @see FileNameUtil#getName(String)
|
||||
* @since 4.1.13
|
||||
*/
|
||||
public static String getName(final String filePath) {
|
||||
return FileNameUtil.getName(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件后缀名,扩展名不带“.”
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 扩展名
|
||||
* @see FileNameUtil#getSuffix(File)
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public static String getSuffix(final File file) {
|
||||
return FileNameUtil.getSuffix(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件后缀名,扩展名不带“.”
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 扩展名
|
||||
* @see FileNameUtil#getSuffix(String)
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public static String getSuffix(final String fileName) {
|
||||
return FileNameUtil.getSuffix(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回主文件名
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 主文件名
|
||||
* @see FileNameUtil#getPrefix(File)
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public static String getPrefix(final File file) {
|
||||
return FileNameUtil.getPrefix(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回主文件名
|
||||
*
|
||||
* @param fileName 完整文件名
|
||||
* @return 主文件名
|
||||
* @see FileNameUtil#getPrefix(String)
|
||||
* @since 5.3.8
|
||||
*/
|
||||
public static String getPrefix(final String fileName) {
|
||||
return FileNameUtil.getPrefix(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回主文件名
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 主文件名
|
||||
* @see FileNameUtil#mainName(File)
|
||||
*/
|
||||
public static String mainName(final File file) {
|
||||
return FileNameUtil.mainName(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回主文件名
|
||||
*
|
||||
* @param fileName 完整文件名
|
||||
* @return 主文件名
|
||||
* @see FileNameUtil#mainName(String)
|
||||
*/
|
||||
public static String mainName(final String fileName) {
|
||||
return FileNameUtil.mainName(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名(后缀名),扩展名不带“.”
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 扩展名
|
||||
* @see FileNameUtil#extName(File)
|
||||
*/
|
||||
public static String extName(final File file) {
|
||||
return FileNameUtil.extName(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件的扩展名(后缀名),扩展名不带“.”
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 扩展名
|
||||
* @see FileNameUtil#extName(String)
|
||||
*/
|
||||
public static String extName(final String fileName) {
|
||||
return FileNameUtil.extName(fileName);
|
||||
}
|
||||
// -------------------------------------------------------------------------------------------- name end
|
||||
|
||||
/**
|
||||
* 判断文件路径是否有指定后缀,忽略大小写<br>
|
||||
* 常用语判断扩展名
|
||||
@@ -2918,30 +2722,6 @@ public class FileUtil extends PathUtil {
|
||||
return FileWriter.of(file, charset).writeLines(lines, lineSeparator, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除文件名中的在Windows下不支持的非法字符,包括: \ / : * ? " < > |
|
||||
*
|
||||
* @param fileName 文件名(必须不包括路径,否则路径符将被替换)
|
||||
* @return 清理后的文件名
|
||||
* @see FileNameUtil#cleanInvalid(String)
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public static String cleanInvalid(final String fileName) {
|
||||
return FileNameUtil.cleanInvalid(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件名中是否包含在Windows下不支持的非法字符,包括: \ / : * ? " < > |
|
||||
*
|
||||
* @param fileName 文件名(必须不包括路径,否则路径符将被替换)
|
||||
* @return 是否包含非法字符
|
||||
* @see FileNameUtil#containsInvalid(String)
|
||||
* @since 3.3.1
|
||||
*/
|
||||
public static boolean containsInvalid(final String fileName) {
|
||||
return FileNameUtil.containsInvalid(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Web项目下的web root路径<br>
|
||||
* 原理是首先获取ClassPath路径,由于在web项目中ClassPath位于 WEB-INF/classes/下,故向上获取两级目录即可。
|
||||
|
104
hutool-core/src/main/java/cn/hutool/core/io/file/PathDeleter.java
Executable file
104
hutool-core/src/main/java/cn/hutool/core/io/file/PathDeleter.java
Executable file
@@ -0,0 +1,104 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.file.visitor.DelVisitor;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 文件删除封装
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class PathDeleter {
|
||||
|
||||
/**
|
||||
* 创建文件或目录移动器
|
||||
*
|
||||
* @param src 源文件或目录
|
||||
* @return {@code PathMover}
|
||||
*/
|
||||
public static PathDeleter of(final Path src) {
|
||||
return new PathDeleter(src);
|
||||
}
|
||||
|
||||
private final Path path;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param path 文件或目录,不能为{@code null}且必须存在
|
||||
*/
|
||||
public PathDeleter(final Path path) {
|
||||
this.path = Assert.notNull(path, "Path must be not null !");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件或者文件夹,不追踪软链<br>
|
||||
* 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹<br>
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public void del() throws IORuntimeException {
|
||||
final Path path = this.path;
|
||||
if (Files.notExists(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (PathUtil.isDirectory(path)) {
|
||||
_del(path);
|
||||
} else {
|
||||
delFile(path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空目录
|
||||
*/
|
||||
public void clean() {
|
||||
try (final Stream<Path> list = Files.list(this.path)){
|
||||
list.forEach(PathUtil::del);
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除目录
|
||||
*
|
||||
* @param path 目录路径
|
||||
*/
|
||||
private static void _del(final Path path) {
|
||||
try {
|
||||
Files.walkFileTree(path, DelVisitor.INSTANCE);
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件或空目录,不追踪软链
|
||||
*
|
||||
* @param path 文件对象
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 5.7.7
|
||||
*/
|
||||
private static void delFile(final Path path) throws IORuntimeException {
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (final IOException e) {
|
||||
if (e instanceof AccessDeniedException) {
|
||||
// 可能遇到只读文件,无法删除.使用 file 方法删除
|
||||
if (path.toFile().delete()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -67,7 +67,7 @@ public class PathMover {
|
||||
* <li>如果src和target为同一文件或目录,直接返回target。</li>
|
||||
* <li>如果src为文件,target为目录,则移动到目标目录下,存在同名文件则按照是否覆盖参数执行。</li>
|
||||
* <li>如果src为文件,target为文件,则按照是否覆盖参数执行。</li>
|
||||
* <li>如果src为文件,target为不存在的路径,则重命名源文件到目标指定的文件,如moveContent("/a/b", "/c/d"), d不存在,则b变成d。</li>
|
||||
* <li>如果src为文件,target为不存在的路径,则重命名源文件到目标指定的文件,如move("/a/b", "/c/d"), d不存在,则b变成d。</li>
|
||||
* <li>如果src为目录,target为文件,抛出{@link IllegalArgumentException}</li>
|
||||
* <li>如果src为目录,target为目录,则将源目录及其内容移动到目标路径目录中,如move("/a/b", "/c/d"),结果为"/c/d/b"</li>
|
||||
* <li>如果src为目录,target为不存在的路径,则重命名src到target,如move("/a/b", "/c/d"),结果为"/c/d/",相当于b重命名为d</li>
|
||||
|
@@ -2,7 +2,6 @@ package cn.hutool.core.io.file;
|
||||
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.visitor.DelVisitor;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
@@ -127,25 +126,11 @@ public class PathUtil {
|
||||
* 某个文件删除失败会终止删除操作
|
||||
*
|
||||
* @param path 文件对象
|
||||
* @return 成功与否
|
||||
* @throws IORuntimeException IO异常
|
||||
* @since 4.4.2
|
||||
*/
|
||||
public static boolean del(final Path path) throws IORuntimeException {
|
||||
if (Files.notExists(path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
if (isDirectory(path)) {
|
||||
Files.walkFileTree(path, DelVisitor.INSTANCE);
|
||||
} else {
|
||||
delFile(path);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
return true;
|
||||
public static void del(final Path path) throws IORuntimeException {
|
||||
PathDeleter.of(path).del();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,11 +139,7 @@ public class PathUtil {
|
||||
* @param path 目录路径
|
||||
*/
|
||||
public static void clean(final Path path) {
|
||||
try {
|
||||
Files.walkFileTree(path, DelVisitor.INSTANCE);
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
PathDeleter.of(path).clean();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -617,22 +598,4 @@ public class PathUtil {
|
||||
}
|
||||
return path.getFileName().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件或空目录,不追踪软链
|
||||
*
|
||||
* @param path 文件对象
|
||||
* @throws IOException IO异常
|
||||
* @since 5.7.7
|
||||
*/
|
||||
protected static void delFile(final Path path) throws IOException {
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (final AccessDeniedException e) {
|
||||
// 可能遇到只读文件,无法删除.使用 file 方法删除
|
||||
if (false == path.toFile().delete()) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.io.file.visitor;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.core.io.resource;
|
||||
|
||||
import cn.hutool.core.classloader.ClassLoaderUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.net.url.URLUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
@@ -67,7 +68,7 @@ public class ClassPathResource extends UrlResource {
|
||||
|
||||
final String path = normalizePath(pathBaseClassLoader);
|
||||
this.path = path;
|
||||
this.name = StrUtil.isBlank(path) ? null : FileUtil.getName(path);
|
||||
this.name = StrUtil.isBlank(path) ? null : FileNameUtil.getName(path);
|
||||
|
||||
this.classLoader = ObjUtil.defaultIfNull(classLoader, ClassLoaderUtil::getClassLoader);
|
||||
this.clazz = clazz;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.io.resource;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.net.url.URLUtil;
|
||||
|
||||
@@ -50,7 +51,7 @@ public class UrlResource implements Resource, Serializable{
|
||||
if(null != url && URLUtil.URL_PROTOCOL_FILE.equals(url.getProtocol())){
|
||||
this.lastModified = FileUtil.file(url).lastModified();
|
||||
}
|
||||
this.name = ObjUtil.defaultIfNull(name, () -> (null != url ? FileUtil.getName(url.getPath()) : null));
|
||||
this.name = ObjUtil.defaultIfNull(name, () -> (null != url ? FileNameUtil.getName(url.getPath()) : null));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------- Constructor end
|
||||
|
@@ -2,6 +2,7 @@ package cn.hutool.core.net.multipart;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
@@ -247,7 +248,7 @@ public class UploadFile {
|
||||
return isAllow;
|
||||
}
|
||||
|
||||
final String fileNameExt = FileUtil.extName(this.getFileName());
|
||||
final String fileNameExt = FileNameUtil.extName(this.getFileName());
|
||||
for (final String fileExtension : setting.fileExts) {
|
||||
if (fileNameExt.equalsIgnoreCase(fileExtension)) {
|
||||
return isAllow;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.io.file.LineSeparator;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
@@ -53,22 +54,6 @@ public class FileUtilTest {
|
||||
FileUtil.touch("d:\\tea\\a.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void delTest() {
|
||||
// 删除一个不存在的文件,应返回true
|
||||
final boolean result = FileUtil.del("e:/Hutool_test_3434543533409843.txt");
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void delTest2() {
|
||||
// 删除一个不存在的文件,应返回true
|
||||
final boolean result = FileUtil.del(Paths.get("e:/Hutool_test_3434543533409843.txt"));
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void renameTest() {
|
||||
@@ -317,61 +302,61 @@ public class FileUtilTest {
|
||||
@Test
|
||||
public void getNameTest() {
|
||||
String path = "d:\\aaa\\bbb\\cc\\ddd\\";
|
||||
String name = FileUtil.getName(path);
|
||||
String name = FileNameUtil.getName(path);
|
||||
Assert.assertEquals("ddd", name);
|
||||
|
||||
path = "d:\\aaa\\bbb\\cc\\ddd.jpg";
|
||||
name = FileUtil.getName(path);
|
||||
name = FileNameUtil.getName(path);
|
||||
Assert.assertEquals("ddd.jpg", name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mainNameTest() {
|
||||
String path = "d:\\aaa\\bbb\\cc\\ddd\\";
|
||||
String mainName = FileUtil.mainName(path);
|
||||
String mainName = FileNameUtil.mainName(path);
|
||||
Assert.assertEquals("ddd", mainName);
|
||||
|
||||
path = "d:\\aaa\\bbb\\cc\\ddd";
|
||||
mainName = FileUtil.mainName(path);
|
||||
mainName = FileNameUtil.mainName(path);
|
||||
Assert.assertEquals("ddd", mainName);
|
||||
|
||||
path = "d:\\aaa\\bbb\\cc\\ddd.jpg";
|
||||
mainName = FileUtil.mainName(path);
|
||||
mainName = FileNameUtil.mainName(path);
|
||||
Assert.assertEquals("ddd", mainName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extNameTest() {
|
||||
String path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\ddd\\" : "~/Desktop/hutool/ddd/";
|
||||
String mainName = FileUtil.extName(path);
|
||||
String mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\ddd" : "~/Desktop/hutool/ddd";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\ddd.jpg" : "~/Desktop/hutool/ddd.jpg";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("jpg", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\fff.xlsx" : "~/Desktop/hutool/fff.xlsx";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("xlsx", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\fff.tar.gz" : "~/Desktop/hutool/fff.tar.gz";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("tar.gz", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\fff.tar.Z" : "~/Desktop/hutool/fff.tar.Z";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("tar.Z", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\fff.tar.bz2" : "~/Desktop/hutool/fff.tar.bz2";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("tar.bz2", mainName);
|
||||
|
||||
path = FileUtil.isWindows() ? "d:\\aaa\\bbb\\cc\\fff.tar.xz" : "~/Desktop/hutool/fff.tar.xz";
|
||||
mainName = FileUtil.extName(path);
|
||||
mainName = FileNameUtil.extName(path);
|
||||
Assert.assertEquals("tar.xz", mainName);
|
||||
}
|
||||
|
||||
@@ -445,10 +430,10 @@ public class FileUtilTest {
|
||||
Assert.assertTrue(nullDirTempFile.exists());
|
||||
|
||||
final File suffixDirTempFile = FileUtil.createTempFile(".xlsx",true);
|
||||
Assert.assertEquals("xlsx", FileUtil.getSuffix(suffixDirTempFile));
|
||||
Assert.assertEquals("xlsx", FileNameUtil.getSuffix(suffixDirTempFile));
|
||||
|
||||
final File prefixDirTempFile = FileUtil.createTempFile("prefix",".xlsx",true);
|
||||
Assert.assertTrue(FileUtil.getPrefix(prefixDirTempFile).startsWith("prefix"));
|
||||
Assert.assertTrue(FileNameUtil.getPrefix(prefixDirTempFile).startsWith("prefix"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
@@ -11,6 +12,7 @@ import java.nio.file.Paths;
|
||||
public class PathCopyTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copySameFileTest() {
|
||||
final Path path = Paths.get("d:/test/dir1/test1.txt");
|
||||
//src路径和target路径相同时,不执行操作
|
||||
@@ -20,6 +22,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copySameDirTest() {
|
||||
final Path path = Paths.get("d:/test/dir1");
|
||||
//src路径和target路径相同时,不执行操作
|
||||
@@ -29,6 +32,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyFileToDirTest() {
|
||||
// src为文件,target为已存在目录,则拷贝到目录下,文件名不变。
|
||||
PathUtil.copy(
|
||||
@@ -37,6 +41,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyFileToPathNotExistTest() {
|
||||
// src为文件,target为不存在路径,则目标以文件对待(自动创建父级目录)
|
||||
// 相当于拷贝后重命名
|
||||
@@ -46,6 +51,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyFileToFileTest() {
|
||||
//src为文件,target是一个已存在的文件,则当{@link CopyOption}设为覆盖时会被覆盖,默认不覆盖。
|
||||
PathUtil.copy(
|
||||
@@ -54,6 +60,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyDirToDirTest() {
|
||||
//src为目录,target为已存在目录,整个src目录连同其目录拷贝到目标目录中
|
||||
PathUtil.copy(
|
||||
@@ -62,6 +69,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyDirToPathNotExistTest() {
|
||||
//src为目录,target为不存在路径,则自动创建目标为新目录,整个src目录连同其目录拷贝到目标目录中
|
||||
PathUtil.copy(
|
||||
@@ -70,6 +78,7 @@ public class PathCopyTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyDirToFileTest() {
|
||||
//src为目录,target为文件,抛出IllegalArgumentException
|
||||
PathUtil.copy(
|
||||
|
28
hutool-core/src/test/java/cn/hutool/core/io/file/PathDeleterTest.java
Executable file
28
hutool-core/src/test/java/cn/hutool/core/io/file/PathDeleterTest.java
Executable file
@@ -0,0 +1,28 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class PathDeleterTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void delFileTest() {
|
||||
FileUtil.touch("d:/test/exist.txt");
|
||||
PathUtil.del(Paths.get("d:/test/exist.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void delDirTest() {
|
||||
PathUtil.del(Paths.get("d:/test/dir1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void cleanDirTest() {
|
||||
PathUtil.clean(Paths.get("d:/test/dir1"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user