Class FileUtil

java.lang.Object
com.sure.tool.io.FileUtil

public class FileUtil extends Object
文件工具类,参考 Hutool 的 FileUtil 设计。

English: File creation, reading, copying, moving and deletion helpers with clear null and exception semantics.

Since:
0.1.0
Author:
suretool
  • Method Details

    • file

      public static File file(String... pathParts)
      拼接路径为 File。
      Parameters:
      pathParts - 路径片段
      Returns:
      File
    • exists

      public static boolean exists(File file)
      文件或目录是否存在。
      Parameters:
      file - 文件
      Returns:
      是否存在
    • exists

      public static boolean exists(String path)
      文件或目录是否存在。
      Parameters:
      path - 路径
      Returns:
      是否存在
    • isFile

      public static boolean isFile(File file)
      是否为文件。
      Parameters:
      file - 文件
      Returns:
      是否为文件
    • isDirectory

      public static boolean isDirectory(File file)
      是否为目录。
      Parameters:
      file - 文件
      Returns:
      是否为目录
    • mkdir

      public static void mkdir(File dir)
      创建目录(含父目录)。
      Parameters:
      dir - 目录
    • touch

      public static File touch(File file) throws IOException
      创建文件(自动创建父目录,已存在则跳过)。
      Parameters:
      file - 文件
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • getName

      public static String getName(File file)
      获取文件名。
      Parameters:
      file - 文件
      Returns:
      文件名
    • getExt

      public static String getExt(File file)
      获取文件扩展名(不含点,小写)。
      Parameters:
      file - 文件
      Returns:
      扩展名,无扩展名返回空串
    • getExt

      public static String getExt(String fileName)
      获取文件扩展名(不含点)。
      Parameters:
      fileName - 文件名
      Returns:
      扩展名,无扩展名返回空串
    • getMimeType

      public static String getMimeType(String fileName)
      根据文件扩展名获取常用 MIME 类型(不区分大小写)。
      Parameters:
      fileName - 文件名
      Returns:
      MIME 类型;未知扩展名返回 application/octet-stream,无扩展名返回 null
    • getParent

      public static String getParent(String path)
      获取路径的父目录路径(不含末尾分隔符)。
      Parameters:
      path - 路径
      Returns:
      父目录路径,无父目录返回 null
    • getParent

      public static String getParent(File file)
      获取文件的父目录绝对路径。
      Parameters:
      file - 文件
      Returns:
      父目录绝对路径,无父目录返回 null
    • isDirEmpty

      public static boolean isDirEmpty(File dir)
      目录是否为空(不存在或不是目录返回 false)。
      Parameters:
      dir - 目录
      Returns:
      是否为空
    • pathEquals

      public static boolean pathEquals(String path1, String path2)
      比较两个路径是否指向同一位置(规范化后比较,忽略 . / .. 与尾分隔符差异)。
      Parameters:
      path1 - 路径 1
      path2 - 路径 2
      Returns:
      是否相同
    • mainName

      public static String mainName(File file)
      获取主文件名(不含扩展名)。
      Parameters:
      file - 文件
      Returns:
      主文件名
    • mainName

      public static String mainName(String fileName)
      获取主文件名(不含扩展名)。
      Parameters:
      fileName - 文件名
      Returns:
      主文件名
    • getAbsolutePath

      public static String getAbsolutePath(File file)
      获取绝对路径。
      Parameters:
      file - 文件
      Returns:
      绝对路径
    • ls

      public static File[] ls(File dir)
      列出目录下的直接子项。
      Parameters:
      dir - 目录
      Returns:
      子项数组,目录无效返回 null
    • loopFiles

      public static List<File> loopFiles(File dir)
      递归列出目录下全部文件。
      Parameters:
      dir - 目录
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(String path)
      递归列出目录下全部文件。
      Parameters:
      path - 目录路径
      Returns:
      文件列表
    • listFileNames

      public static List<String> listFileNames(String path)
      列出目录下的文件名(仅文件)。
      Parameters:
      path - 目录路径
      Returns:
      文件名列表
    • readUtf8String

      public static String readUtf8String(File file) throws IOException
      按 UTF-8 读取文件为字符串。
      Parameters:
      file - 文件
      Returns:
      字符串
      Throws:
      IOException - IO 异常
    • readString

      public static String readString(File file, Charset charset) throws IOException
      按指定字符集读取文件为字符串。
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      字符串
      Throws:
      IOException - IO 异常
    • readUtf8Lines

      public static List<String> readUtf8Lines(File file) throws IOException
      按 UTF-8 读取文件为行列表。
      Parameters:
      file - 文件
      Returns:
      行列表
      Throws:
      IOException - IO 异常
    • readLines

      public static List<String> readLines(File file, Charset charset) throws IOException
      按指定字符集读取文件为行列表。
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      行列表
      Throws:
      IOException - IO 异常
    • readBytes

      public static byte[] readBytes(File file) throws IOException
      读取文件全部字节。
      Parameters:
      file - 文件
      Returns:
      字节数组
      Throws:
      IOException - IO 异常
    • writeUtf8String

      public static File writeUtf8String(String content, File file) throws IOException
      按 UTF-8 写入字符串(覆盖)。
      Parameters:
      content - 内容
      file - 文件
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • writeString

      public static File writeString(String content, File file, Charset charset) throws IOException
      按指定字符集写入字符串(覆盖)。
      Parameters:
      content - 内容
      file - 文件
      charset - 字符集
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • appendUtf8String

      public static File appendUtf8String(String content, File file) throws IOException
      按 UTF-8 追加写入字符串。
      Parameters:
      content - 内容
      file - 文件
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • appendString

      public static File appendString(String content, File file, Charset charset) throws IOException
      按指定字符集追加写入字符串。
      Parameters:
      content - 内容
      file - 文件
      charset - 字符集
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • writeUtf8Lines

      public static File writeUtf8Lines(Collection<?> lines, File file) throws IOException
      按 UTF-8 写入行列表(每行后跟换行符)。
      Parameters:
      lines - 行列表
      file - 文件
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • writeLines

      public static File writeLines(Collection<?> lines, File file, Charset charset) throws IOException
      按指定字符集写入行列表(每行后跟换行符)。
      Parameters:
      lines - 行列表
      file - 文件
      charset - 字符集
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • writeBytes

      public static File writeBytes(byte[] data, File file) throws IOException
      写入字节数组。
      Parameters:
      data - 字节数组
      file - 文件
      Returns:
      文件
      Throws:
      IOException - IO 异常
    • copy

      public static File copy(File src, File dest) throws IOException
      复制文件或目录(目录递归复制)。
      Parameters:
      src - 源文件
      dest - 目标文件
      Returns:
      目标文件
      Throws:
      IOException - IO 异常
    • move

      public static File move(File src, File dest) throws IOException
      移动文件或目录。
      Parameters:
      src - 源文件
      dest - 目标文件
      Returns:
      目标文件
      Throws:
      IOException - IO 异常
    • delete

      public static boolean delete(File file)
      删除文件或目录(目录递归删除)。
      Parameters:
      file - 文件或目录
      Returns:
      是否删除成功
    • copyDir

      public static File copyDir(File srcDir, File destDir) throws IOException
      递归复制目录到目标位置(目标目录不存在时自动创建)。
      Parameters:
      srcDir - 源目录
      destDir - 目标目录
      Returns:
      目标目录
      Throws:
      IOException - IO 异常
    • clean

      public static void clean(File dir)
      清空目录内容(保留目录本身)。
      Parameters:
      dir - 目录
    • size

      public static long size(File file)
      计算文件或目录大小(字节)。
      Parameters:
      file - 文件或目录
      Returns:
      字节数
    • readableFileSize

      public static String readableFileSize(long size)
      文件大小可读化:1024 → 1 KB,1536 → 1.5 KB。
      Parameters:
      size - 字节数
      Returns:
      可读大小
    • normalize

      public static String normalize(String path)
      规范化路径:统一斜杠、去除 ./ 与多余的 ../。
      Parameters:
      path - 路径
      Returns:
      规范化后的路径
    • getTmpDirPath

      public static String getTmpDirPath()
      系统临时目录路径。
      Returns:
      临时目录路径
    • getTmpDir

      public static File getTmpDir()
      系统临时目录。
      Returns:
      临时目录
    • getUserHomePath

      public static String getUserHomePath()
      用户主目录路径。
      Returns:
      主目录路径
    • sizeFormat

      public static String sizeFormat(long size)
      文件大小格式化:B / KB / MB / GB / TB(二进制单位,保留 1 位小数;不足 1KB 显示整数字节)。
      Parameters:
      size - 字节数(负数按 0 处理)
      Returns:
      格式化文本,如 1.5 MB
    • size

      public static long size(String path)
      文件大小(字节),路径便捷重载。
      Parameters:
      path - 文件路径
      Returns:
      字节数;文件不存在或为目录时返回 0
    • copy

      public static File copy(String srcPath, String destPath) throws IOException
      复制文件(自动创建父目录),路径便捷重载。
      Parameters:
      srcPath - 源路径
      destPath - 目标路径
      Returns:
      目标文件
      Throws:
      IOException - 复制失败
    • readLines

      public static List<String> readLines(String path) throws IOException
      按行读取文件(UTF-8),路径便捷重载。
      Parameters:
      path - 文件路径
      Returns:
      行列表
      Throws:
      IOException - 读取失败
    • touch

      public static File touch(String path) throws IOException
      创建文件(含父目录),路径便捷重载。
      Parameters:
      path - 文件路径
      Returns:
      文件
      Throws:
      IOException - 创建失败
    • walkFiles

      public static List<Path> walkFiles(Path root)
      深度遍历目录返回全部文件(不含目录本身),使用 Files.walkFileTree(Path, Set, int, FileVisitor)。
      Parameters:
      root - 根路径(目录或文件)
      Returns:
      文件列表;root 为 null 或不存在时返回空列表
      Throws:
      UncheckedIOException - 遍历 IO 失败
    • lastModified

      public static long lastModified(File file)
      文件最后修改时间(毫秒)。
      Parameters:
      file - 文件
      Returns:
      毫秒时间戳;文件不存在时返回 0
    • isNewer

      public static boolean isNewer(File file, File reference)
      判断文件是否比参考文件新(最后修改时间晚)。
      Parameters:
      file - 待判断文件
      reference - 参考文件
      Returns:
      若 file 更新返回 true;任一文件不存在返回 false
    • isOlder

      public static boolean isOlder(File file, File reference)
      判断文件是否比参考文件旧(最后修改时间早)。
      Parameters:
      file - 待判断文件
      reference - 参考文件
      Returns:
      若 file 更旧返回 true;任一文件不存在返回 false
    • lastModifiedTime

      public static Date lastModifiedTime(File file)
      最后修改时间。
      Parameters:
      file - 文件
      Returns:
      最后修改时间,空文件返回 null
    • checksumMD5

      public static String checksumMD5(File file) throws IOException
      计算文件 MD5 摘要(十六进制小写)。

      示例:FileUtil.checksumMD5(file) 返回 32 位小写 hex。

      Parameters:
      file - 文件
      Returns:
      MD5 hex 字符串
      Throws:
      IOException - 文件不存在或读取失败
    • checksumSHA1

      public static String checksumSHA1(File file) throws IOException
      计算文件 SHA-1 摘要(十六进制小写)。

      示例:FileUtil.checksumSHA1(file) 返回 40 位小写 hex。

      Parameters:
      file - 文件
      Returns:
      SHA-1 hex 字符串
      Throws:
      IOException - 文件不存在或读取失败
    • checksumCRC32

      public static long checksumCRC32(File file) throws IOException
      计算文件 CRC32 校验和。

      示例:FileUtil.checksumCRC32(file) 返回无符号 long。

      Parameters:
      file - 文件
      Returns:
      CRC32 值(无符号)
      Throws:
      IOException - 文件不存在或读取失败
    • isSymlink

      public static boolean isSymlink(File file)
      判断文件是否为符号链接。

      示例:FileUtil.isSymlink(file);对不存在路径返回 false。

      Parameters:
      file - 文件或目录
      Returns:
      是符号链接返回 true
    • isSymlink

      public static boolean isSymlink(String path)
      判断路径是否为符号链接。
      Parameters:
      path - 路径字符串
      Returns:
      是符号链接返回 true
    • getCanonicalPath

      public static String getCanonicalPath(File file)
      获取规范化绝对路径(解析 . 与 ..,跟随符号链接),失败返回原始绝对路径。

      示例:FileUtil.getCanonicalPath(file)。

      Parameters:
      file - 文件
      Returns:
      规范化路径;null 返回 null
    • getCanonicalPath

      public static String getCanonicalPath(String path)
      获取规范化绝对路径。
      Parameters:
      path - 路径字符串
      Returns:
      规范化路径;null 返回 null
    • openInputStream

      public static InputStream openInputStream(File file)
      打开文件输入流(IOException 包装为 UncheckedIOException)。

      示例:try (InputStream in = FileUtil.openInputStream(file)) { ... }。

      Parameters:
      file - 文件
      Returns:
      文件输入流
      Throws:
      UncheckedIOException - 文件不存在或不可读
    • openOutputStream

      public static OutputStream openOutputStream(File file)
      打开文件输出流(覆盖写,IOException 包装为 UncheckedIOException)。

      示例:try (OutputStream out = FileUtil.openOutputStream(file)) { ... }。

      Parameters:
      file - 文件
      Returns:
      文件输出流
      Throws:
      UncheckedIOException - 文件不可写
    • deleteQuietly

      public static boolean deleteQuietly(File file)
      静默删除文件或空目录:删除失败不抛异常。

      示例:FileUtil.deleteQuietly(file);目录非空时静默失败。

      Parameters:
      file - 文件或目录,可为 null
      Returns:
      删除成功返回 true;null 或不存在的文件返回 false