FileTypeUtil.getType增加文件判断

This commit is contained in:
Looly 2023-05-19 21:51:55 +08:00
parent 87fd7393f9
commit 1035723be7
3 changed files with 24 additions and 10 deletions

View File

@ -12,14 +12,14 @@
package org.dromara.hutool.core.io; package org.dromara.hutool.core.io;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.exception.HutoolException;
/** /**
* IO运行时异常常用于对IOException的包装 * IO运行时异常常用于对IOException的包装
* *
* @author xiaoleilu * @author looly
*/ */
public class IORuntimeException extends RuntimeException { public class IORuntimeException extends HutoolException {
private static final long serialVersionUID = 8247610319171014183L; private static final long serialVersionUID = 8247610319171014183L;
/** /**
@ -54,7 +54,7 @@ public class IORuntimeException extends RuntimeException {
* @param params 参数 * @param params 参数
*/ */
public IORuntimeException(final String messageTemplate, final Object... params) { public IORuntimeException(final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params)); super(messageTemplate, params);
} }
/** /**
@ -87,7 +87,7 @@ public class IORuntimeException extends RuntimeException {
* @param params 参数 * @param params 参数
*/ */
public IORuntimeException(final Throwable cause, final String messageTemplate, final Object... params) { public IORuntimeException(final Throwable cause, final String messageTemplate, final Object... params) {
super(StrUtil.format(messageTemplate, params), cause); super(cause, messageTemplate, params);
} }
/** /**

View File

@ -208,6 +208,9 @@ public class FileTypeUtil {
* @throws IORuntimeException 读取文件引起的异常 * @throws IORuntimeException 读取文件引起的异常
*/ */
public static String getType(final File file, final boolean isExact) throws IORuntimeException { public static String getType(final File file, final boolean isExact) throws IORuntimeException {
if(false == FileUtil.isFile(file)){
throw new IllegalArgumentException("Not a regular file!");
}
InputStream in = null; InputStream in = null;
try { try {
in = IoUtil.toStream(file); in = IoUtil.toStream(file);

View File

@ -512,6 +512,17 @@ public class PathUtil {
return Files.isSymbolicLink(path); return Files.isSymbolicLink(path);
} }
/**
* 判断是否为其它类型文件即非文件非目录非链接
*
* @param path 被检查的文件非空
* @return 是否为其它类型文件
* @since 6.0.0
*/
public static boolean isOther(final Path path) {
return getAttributes(path, false).isOther();
}
/** /**
* 判断文件或目录是否存在 * 判断文件或目录是否存在
* *
@ -615,9 +626,9 @@ public class PathUtil {
* 创建临时文件<br> * 创建临时文件<br>
* 创建后的文件名为 prefix[Random].suffix From com.jodd.io.FileUtil * 创建后的文件名为 prefix[Random].suffix From com.jodd.io.FileUtil
* *
* @param prefix 前缀至少3个字符 * @param prefix 前缀至少3个字符
* @param suffix 后缀如果null则使用默认.tmp * @param suffix 后缀如果null则使用默认.tmp
* @param dir 临时文件创建的所在目录 * @param dir 临时文件创建的所在目录
* @return 临时文件 * @return 临时文件
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 6.0.0 * @since 6.0.0
@ -626,9 +637,9 @@ public class PathUtil {
int exceptionsCount = 0; int exceptionsCount = 0;
while (true) { while (true) {
try { try {
if(null == dir){ if (null == dir) {
return Files.createTempFile(prefix, suffix); return Files.createTempFile(prefix, suffix);
}else{ } else {
return Files.createTempFile(mkdir(dir), prefix, suffix); return Files.createTempFile(mkdir(dir), prefix, suffix);
} }
} catch (final IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied } catch (final IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied