Class FileUtil
java.lang.Object
com.sure.tool.io.FileUtil
文件工具类,参考 Hutool 的
FileUtil 设计。
English: File creation, reading, copying, moving and deletion helpers with clear null and exception semantics.
- Since:
- 0.1.0
- Author:
- suretool
-
Method Summary
Modifier and TypeMethodDescriptionstatic FileappendString(String content, File file, Charset charset) 按指定字符集追加写入字符串。static FileappendUtf8String(String content, File file) 按 UTF-8 追加写入字符串。static longchecksumCRC32(File file) 计算文件 CRC32 校验和。static StringchecksumMD5(File file) 计算文件 MD5 摘要(十六进制小写)。static StringchecksumSHA1(File file) 计算文件 SHA-1 摘要(十六进制小写)。static void清空目录内容(保留目录本身)。static File复制文件或目录(目录递归复制)。static File复制文件(自动创建父目录),路径便捷重载。static File递归复制目录到目标位置(目标目录不存在时自动创建)。static boolean删除文件或目录(目录递归删除)。static booleandeleteQuietly(File file) 静默删除文件或空目录:删除失败不抛异常。static boolean文件或目录是否存在。static boolean文件或目录是否存在。static File拼接路径为 File。static StringgetAbsolutePath(File file) 获取绝对路径。static StringgetCanonicalPath(File file) 获取规范化绝对路径(解析 .static StringgetCanonicalPath(String path) 获取规范化绝对路径。static String获取文件扩展名(不含点,小写)。static String获取文件扩展名(不含点)。static StringgetMimeType(String fileName) 根据文件扩展名获取常用 MIME 类型(不区分大小写)。static String获取文件名。static String获取文件的父目录绝对路径。static String获取路径的父目录路径(不含末尾分隔符)。static File系统临时目录。static String系统临时目录路径。static String用户主目录路径。static booleanisDirectory(File file) 是否为目录。static booleanisDirEmpty(File dir) 目录是否为空(不存在或不是目录返回false)。static boolean是否为文件。static boolean判断文件是否比参考文件新(最后修改时间晚)。static boolean判断文件是否比参考文件旧(最后修改时间早)。static boolean判断文件是否为符号链接。static boolean判断路径是否为符号链接。static longlastModified(File file) 文件最后修改时间(毫秒)。static DatelastModifiedTime(File file) 最后修改时间。listFileNames(String path) 列出目录下的文件名(仅文件)。递归列出目录下全部文件。递归列出目录下全部文件。static File[]列出目录下的直接子项。static String获取主文件名(不含扩展名)。static String获取主文件名(不含扩展名)。static void创建目录(含父目录)。static File移动文件或目录。static String规范化路径:统一斜杠、去除./与多余的../。static InputStreamopenInputStream(File file) 打开文件输入流(IOException 包装为UncheckedIOException)。static OutputStreamopenOutputStream(File file) 打开文件输出流(覆盖写,IOException 包装为UncheckedIOException)。static booleanpathEquals(String path1, String path2) 比较两个路径是否指向同一位置(规范化后比较,忽略./..与尾分隔符差异)。static StringreadableFileSize(long size) 文件大小可读化:1024→1 KB,1536→1.5 KB。static byte[]读取文件全部字节。按指定字符集读取文件为行列表。按行读取文件(UTF-8),路径便捷重载。static StringreadString(File file, Charset charset) 按指定字符集读取文件为字符串。readUtf8Lines(File file) 按 UTF-8 读取文件为行列表。static StringreadUtf8String(File file) 按 UTF-8 读取文件为字符串。static long计算文件或目录大小(字节)。static long文件大小(字节),路径便捷重载。static StringsizeFormat(long size) 文件大小格式化:B / KB / MB / GB / TB(二进制单位,保留 1 位小数;不足 1KB 显示整数字节)。static File创建文件(自动创建父目录,已存在则跳过)。static File创建文件(含父目录),路径便捷重载。深度遍历目录返回全部文件(不含目录本身),使用Files.walkFileTree(Path, Set, int, FileVisitor)。static FilewriteBytes(byte[] data, File file) 写入字节数组。static FilewriteLines(Collection<?> lines, File file, Charset charset) 按指定字符集写入行列表(每行后跟换行符)。static FilewriteString(String content, File file, Charset charset) 按指定字符集写入字符串(覆盖)。static FilewriteUtf8Lines(Collection<?> lines, File file) 按 UTF-8 写入行列表(每行后跟换行符)。static FilewriteUtf8String(String content, File file) 按 UTF-8 写入字符串(覆盖)。
-
Method Details
-
file
-
exists
-
exists
-
isFile
-
isDirectory
-
mkdir
-
touch
创建文件(自动创建父目录,已存在则跳过)。- Parameters:
file- 文件- Returns:
- 文件
- Throws:
IOException- IO 异常
-
getName
-
getExt
-
getExt
-
getMimeType
-
getParent
-
getParent
-
isDirEmpty
目录是否为空(不存在或不是目录返回false)。- Parameters:
dir- 目录- Returns:
- 是否为空
-
pathEquals
-
mainName
-
mainName
-
getAbsolutePath
-
ls
-
loopFiles
-
loopFiles
-
listFileNames
-
readUtf8String
按 UTF-8 读取文件为字符串。- Parameters:
file- 文件- Returns:
- 字符串
- Throws:
IOException- IO 异常
-
readString
按指定字符集读取文件为字符串。- Parameters:
file- 文件charset- 字符集- Returns:
- 字符串
- Throws:
IOException- IO 异常
-
readUtf8Lines
按 UTF-8 读取文件为行列表。- Parameters:
file- 文件- Returns:
- 行列表
- Throws:
IOException- IO 异常
-
readLines
按指定字符集读取文件为行列表。- Parameters:
file- 文件charset- 字符集- Returns:
- 行列表
- Throws:
IOException- IO 异常
-
readBytes
读取文件全部字节。- Parameters:
file- 文件- Returns:
- 字节数组
- Throws:
IOException- IO 异常
-
writeUtf8String
按 UTF-8 写入字符串(覆盖)。- Parameters:
content- 内容file- 文件- Returns:
- 文件
- Throws:
IOException- IO 异常
-
writeString
按指定字符集写入字符串(覆盖)。- Parameters:
content- 内容file- 文件charset- 字符集- Returns:
- 文件
- Throws:
IOException- IO 异常
-
appendUtf8String
按 UTF-8 追加写入字符串。- Parameters:
content- 内容file- 文件- Returns:
- 文件
- Throws:
IOException- IO 异常
-
appendString
按指定字符集追加写入字符串。- Parameters:
content- 内容file- 文件charset- 字符集- Returns:
- 文件
- Throws:
IOException- IO 异常
-
writeUtf8Lines
按 UTF-8 写入行列表(每行后跟换行符)。- Parameters:
lines- 行列表file- 文件- Returns:
- 文件
- Throws:
IOException- IO 异常
-
writeLines
按指定字符集写入行列表(每行后跟换行符)。- Parameters:
lines- 行列表file- 文件charset- 字符集- Returns:
- 文件
- Throws:
IOException- IO 异常
-
writeBytes
写入字节数组。- Parameters:
data- 字节数组file- 文件- Returns:
- 文件
- Throws:
IOException- IO 异常
-
copy
复制文件或目录(目录递归复制)。- Parameters:
src- 源文件dest- 目标文件- Returns:
- 目标文件
- Throws:
IOException- IO 异常
-
move
移动文件或目录。- Parameters:
src- 源文件dest- 目标文件- Returns:
- 目标文件
- Throws:
IOException- IO 异常
-
delete
-
copyDir
递归复制目录到目标位置(目标目录不存在时自动创建)。- Parameters:
srcDir- 源目录destDir- 目标目录- Returns:
- 目标目录
- Throws:
IOException- IO 异常
-
clean
-
size
-
readableFileSize
文件大小可读化:1024→1 KB,1536→1.5 KB。- Parameters:
size- 字节数- Returns:
- 可读大小
-
normalize
-
getTmpDirPath
-
getTmpDir
-
getUserHomePath
-
sizeFormat
文件大小格式化:B / KB / MB / GB / TB(二进制单位,保留 1 位小数;不足 1KB 显示整数字节)。- Parameters:
size- 字节数(负数按 0 处理)- Returns:
- 格式化文本,如
1.5 MB
-
size
文件大小(字节),路径便捷重载。- Parameters:
path- 文件路径- Returns:
- 字节数;文件不存在或为目录时返回 0
-
copy
复制文件(自动创建父目录),路径便捷重载。- Parameters:
srcPath- 源路径destPath- 目标路径- Returns:
- 目标文件
- Throws:
IOException- 复制失败
-
readLines
按行读取文件(UTF-8),路径便捷重载。- Parameters:
path- 文件路径- Returns:
- 行列表
- Throws:
IOException- 读取失败
-
touch
创建文件(含父目录),路径便捷重载。- Parameters:
path- 文件路径- Returns:
- 文件
- Throws:
IOException- 创建失败
-
walkFiles
深度遍历目录返回全部文件(不含目录本身),使用Files.walkFileTree(Path, Set, int, FileVisitor)。- Parameters:
root- 根路径(目录或文件)- Returns:
- 文件列表;root 为 null 或不存在时返回空列表
- Throws:
UncheckedIOException- 遍历 IO 失败
-
lastModified
文件最后修改时间(毫秒)。- Parameters:
file- 文件- Returns:
- 毫秒时间戳;文件不存在时返回 0
-
isNewer
-
isOlder
-
lastModifiedTime
-
checksumMD5
计算文件 MD5 摘要(十六进制小写)。示例:
FileUtil.checksumMD5(file)返回 32 位小写 hex。- Parameters:
file- 文件- Returns:
- MD5 hex 字符串
- Throws:
IOException- 文件不存在或读取失败
-
checksumSHA1
计算文件 SHA-1 摘要(十六进制小写)。示例:
FileUtil.checksumSHA1(file)返回 40 位小写 hex。- Parameters:
file- 文件- Returns:
- SHA-1 hex 字符串
- Throws:
IOException- 文件不存在或读取失败
-
checksumCRC32
计算文件 CRC32 校验和。示例:
FileUtil.checksumCRC32(file)返回无符号 long。- Parameters:
file- 文件- Returns:
- CRC32 值(无符号)
- Throws:
IOException- 文件不存在或读取失败
-
isSymlink
判断文件是否为符号链接。示例:
FileUtil.isSymlink(file);对不存在路径返回false。- Parameters:
file- 文件或目录- Returns:
- 是符号链接返回
true
-
isSymlink
判断路径是否为符号链接。- Parameters:
path- 路径字符串- Returns:
- 是符号链接返回
true
-
getCanonicalPath
-
getCanonicalPath
-
openInputStream
打开文件输入流(IOException 包装为UncheckedIOException)。示例:
try (InputStream in = FileUtil.openInputStream(file)) { ... }。- Parameters:
file- 文件- Returns:
- 文件输入流
- Throws:
UncheckedIOException- 文件不存在或不可读
-
openOutputStream
打开文件输出流(覆盖写,IOException 包装为UncheckedIOException)。示例:
try (OutputStream out = FileUtil.openOutputStream(file)) { ... }。- Parameters:
file- 文件- Returns:
- 文件输出流
- Throws:
UncheckedIOException- 文件不可写
-
deleteQuietly
静默删除文件或空目录:删除失败不抛异常。示例:
FileUtil.deleteQuietly(file);目录非空时静默失败。- Parameters:
file- 文件或目录,可为null- Returns:
- 删除成功返回
true;null或不存在的文件返回false
-