This commit is contained in:
Looly 2023-03-20 22:53:25 +08:00
parent 6060772c32
commit 248af7e2bc
6 changed files with 35 additions and 55 deletions

View File

@ -252,6 +252,7 @@ public class FileUtil extends PathUtil {
}
}
// region ----- file and newFile
/**
* 创建File对象相当于调用new File()不做任何处理
*
@ -375,6 +376,7 @@ public class FileUtil extends PathUtil {
public static File file(final URL url) {
return new File(URLUtil.toURI(url));
}
// endregion
/**
* 获取临时文件目录
@ -396,13 +398,14 @@ public class FileUtil extends PathUtil {
return file(SystemUtil.getUserHomePath());
}
// region ----- exists
/**
* 判断文件是否存在如果path为null则返回false
*
* @param path 文件路径
* @return 如果存在返回true
*/
public static boolean exist(final String path) {
public static boolean exists(final String path) {
return (null != path) && file(path).exists();
}
@ -412,7 +415,7 @@ public class FileUtil extends PathUtil {
* @param file 文件
* @return 如果存在返回true
*/
public static boolean exist(final File file) {
public static boolean exists(final File file) {
return (null != file) && file.exists();
}
@ -423,7 +426,7 @@ public class FileUtil extends PathUtil {
* @param regexp 文件夹中所包含文件名的正则表达式
* @return 如果存在匹配文件返回true
*/
public static boolean exist(final String directory, final String regexp) {
public static boolean exists(final String directory, final String regexp) {
final File file = new File(directory);
if (false == file.exists()) {
return false;
@ -442,7 +445,9 @@ public class FileUtil extends PathUtil {
}
return false;
}
// endregion
// region ----- lastModifiedTime
/**
* 指定文件最后修改时间
*
@ -450,7 +455,7 @@ public class FileUtil extends PathUtil {
* @return 最后修改时间
*/
public static Date lastModifiedTime(final File file) {
if (false == exist(file)) {
if (false == exists(file)) {
return null;
}
@ -466,6 +471,7 @@ public class FileUtil extends PathUtil {
public static Date lastModifiedTime(final String path) {
return lastModifiedTime(new File(path));
}
// endregion
/**
* 计算目录或文件的总大小<br>
@ -563,6 +569,7 @@ public class FileUtil extends PathUtil {
return file.lastModified() > timeMillis;
}
// region ----- touch
/**
* 创建文件及其父目录如果这个文件存在直接返回这个文件<br>
* 此方法不对File对象类型做判断如果File不存在无法判断其类型
@ -627,6 +634,7 @@ public class FileUtil extends PathUtil {
public static File touch(final String parent, final String path) throws IORuntimeException {
return touch(file(parent, path));
}
// endregion
/**
* 创建所给文件或目录的父目录
@ -774,6 +782,7 @@ public class FileUtil extends PathUtil {
return dir.exists();
}
// region ----- createTempFile
/**
* 创建临时文件<br>
* 创建后的文件名为 prefix[Randon].tmp
@ -879,6 +888,7 @@ public class FileUtil extends PathUtil {
}
}
}
// endregion
/**
* 复制文件或目录<br>
@ -1475,8 +1485,7 @@ public class FileUtil extends PathUtil {
return FileTypeUtil.getType(file);
}
// -------------------------------------------------------------------------------------------- in start
// region ----- in
/**
* 获得输入流
*
@ -1571,9 +1580,9 @@ public class FileUtil extends PathUtil {
public static BufferedReader getReader(final String path, final Charset charset) throws IORuntimeException {
return getReader(file(path), charset);
}
// endregion
// -------------------------------------------------------------------------------------------- in end
// region ----- read
/**
* 读取文件所有数据<br>
* 文件的长度不能超过Integer.MAX_VALUE
@ -2024,8 +2033,9 @@ public class FileUtil extends PathUtil {
public static <T> T read(final File file, final Charset charset, final SerFunction<BufferedReader, T> readerHandler) throws IORuntimeException {
return FileReader.of(file, charset).read(readerHandler);
}
// endregion
// -------------------------------------------------------------------------------------------- out start
// region ----- out
/**
* 获得一个输出流对象
@ -2125,9 +2135,9 @@ public class FileUtil extends PathUtil {
return System.lineSeparator();
// return System.getProperty("line.separator");
}
// endregion
// -------------------------------------------------------------------------------------------- out end
// region ----- write and append
/**
* 将String写入文件覆盖模式字符集为UTF-8
*
@ -2653,6 +2663,7 @@ public class FileUtil extends PathUtil {
public static long writeToStream(final String fullFilePath, final OutputStream out) throws IORuntimeException {
return writeToStream(touch(fullFilePath), out);
}
// endregion
/**
* 可读的文件大小

View File

@ -1,23 +1,17 @@
package cn.hutool.core.io.watch;
import cn.hutool.core.io.file.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.file.PathUtil;
import cn.hutool.core.io.watch.watchers.WatcherChain;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.net.url.URLUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.CharUtil;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchService;
import java.nio.file.*;
/**
* 路径监听器
@ -32,27 +26,6 @@ import java.nio.file.WatchService;
public class WatchMonitor extends WatchServer {
private static final long serialVersionUID = 1L;
/**
* 事件丢失
*/
public static final WatchEvent.Kind<?> OVERFLOW = WatchKind.OVERFLOW.getValue();
/**
* 修改事件
*/
public static final WatchEvent.Kind<?> ENTRY_MODIFY = WatchKind.MODIFY.getValue();
/**
* 创建事件
*/
public static final WatchEvent.Kind<?> ENTRY_CREATE = WatchKind.CREATE.getValue();
/**
* 删除事件
*/
public static final WatchEvent.Kind<?> ENTRY_DELETE = WatchKind.DELETE.getValue();
/**
* 全部事件
*/
public static final WatchEvent.Kind<?>[] EVENTS_ALL = WatchKind.ALL;
/**
* 监听路径必须为目录
*/
@ -245,7 +218,7 @@ public class WatchMonitor extends WatchServer {
* @return WatchMonitor
*/
public static WatchMonitor ofAll(final Path path, final Watcher watcher) {
final WatchMonitor watchMonitor = of(path, EVENTS_ALL);
final WatchMonitor watchMonitor = of(path, WatchKind.ALL);
watchMonitor.setWatcher(watcher);
return watchMonitor;
}
@ -317,7 +290,7 @@ public class WatchMonitor extends WatchServer {
@Override
public void init() throws WatchException {
//获取目录或文件路径
if (false == Files.exists(this.path, LinkOption.NOFOLLOW_LINKS)) {
if (false == PathUtil.exists(this.path, false)) {
// 不存在的路径
final Path lastPathEle = FileUtil.getLastPathEle(this.path);
if (null != lastPathEle) {
@ -330,12 +303,8 @@ public class WatchMonitor extends WatchServer {
}
//创建不存在的目录或父目录
try {
Files.createDirectories(this.path);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
} else if (Files.isRegularFile(this.path, LinkOption.NOFOLLOW_LINKS)) {
PathUtil.mkdir(this.path);
} else if (PathUtil.isFile(this.path, false)) {
// 文件路径
this.filePath = this.path;
this.path = this.filePath.getParent();

View File

@ -250,7 +250,7 @@ public class WatchUtil {
* @return {@link WatchMonitor}
*/
public static WatchMonitor ofAll(final Path path, final int maxDepth, final Watcher watcher) {
final WatchMonitor watchMonitor = of(path, maxDepth, WatchMonitor.EVENTS_ALL);
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.ALL);
watchMonitor.setWatcher(watcher);
return watchMonitor;
}
@ -378,7 +378,7 @@ public class WatchUtil {
* @since 4.5.2
*/
public static WatchMonitor createModify(final Path path, final int maxDepth, final Watcher watcher) {
final WatchMonitor watchMonitor = of(path, maxDepth, WatchMonitor.ENTRY_MODIFY);
final WatchMonitor watchMonitor = of(path, maxDepth, WatchKind.MODIFY.getValue());
watchMonitor.setWatcher(watcher);
return watchMonitor;
}

View File

@ -646,7 +646,7 @@ public class Ftp extends AbstractFtp {
if (false == ftpFile.isDirectory()) {
// 本地不存在文件或者ftp上文件有修改则下载
if (false == FileUtil.exist(destFile)
if (false == FileUtil.exists(destFile)
|| (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) {
download(srcFile, destFile);
}

View File

@ -478,7 +478,7 @@ public class Sftp extends AbstractFtp {
* @since 5.7.6
*/
public void upload(final String remotePath, final File file) {
if (false == FileUtil.exist(file)) {
if (false == FileUtil.exists(file)) {
return;
}
if (file.isDirectory()) {
@ -624,7 +624,7 @@ public class Sftp extends AbstractFtp {
if (false == item.getAttrs().isDir()) {
// 本地不存在文件或者ftp上文件有修改则下载
if (false == FileUtil.exist(destFile)
if (false == FileUtil.exists(destFile)
|| (item.getAttrs().getMTime() > (destFile.lastModified() / 1000))) {
download(srcFile, destFile);
}

View File

@ -26,7 +26,7 @@ public class DocUtil {
*/
public static XWPFDocument create(final File file) {
try {
return FileUtil.exist(file) ? new XWPFDocument(OPCPackage.open(file)) : new XWPFDocument();
return FileUtil.exists(file) ? new XWPFDocument(OPCPackage.open(file)) : new XWPFDocument();
} catch (final InvalidFormatException e) {
throw new POIException(e);
} catch (final IOException e) {