Class FileKit

java.lang.Object
org.aoju.bus.core.toolkit.FileKit

public class FileKit extends Object
文件工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • FileKit

      public FileKit()
  • Method Details

    • isWindows

      public static boolean isWindows()
      是否为Windows环境
      Returns:
      是否为Windows环境
    • isEmpty

      public static boolean isEmpty(File file)
      文件是否为空 目录:里面没有文件时为空 文件:文件大小为0时为空
      Parameters:
      file - 文件
      Returns:
      是否为空, 当提供非目录时, 返回false
    • isNotEmpty

      public static boolean isNotEmpty(File file)
      目录是否为空
      Parameters:
      file - 目录
      Returns:
      是否为空, 当提供非目录时, 返回false
    • isDirEmpty

      public static boolean isDirEmpty(Path dirPath)
      目录是否为空
      Parameters:
      dirPath - 目录
      Returns:
      是否为空
      Throws:
      InternalException - IOException
    • isDirEmpty

      public static boolean isDirEmpty(File dir)
      目录是否为空
      Parameters:
      dir - 目录
      Returns:
      是否为空
    • ls

      public static File[] ls(String path)
      列出指定路径下的目录和文件 给定的绝对路径不能是压缩包中的路径
      Parameters:
      path - 目录绝对路径或者相对路径
      Returns:
      文件列表(包含目录)
    • isFile

      public static boolean isFile(String path)
      判断是否为文件,如果path为null,则返回false
      Parameters:
      path - 文件路径
      Returns:
      如果为文件true
    • isFile

      public static boolean isFile(File file)
      判断是否为文件,如果file为null,则返回false
      Parameters:
      file - 文件
      Returns:
      如果为文件true
    • isFile

      public static boolean isFile(Path path, boolean isFollowLinks)
      判断是否为文件,如果file为null,则返回false
      Parameters:
      path - 文件
      isFollowLinks - 是否跟踪软链(快捷方式)
      Returns:
      如果为文件true
    • isDirectory

      public static boolean isDirectory(String path)
      判断是否为目录,如果path为null,则返回false
      Parameters:
      path - 文件路径
      Returns:
      如果为目录true
    • isDirectory

      public static boolean isDirectory(Path path)
      判断是否为目录,如果file为null,则返回false 此方法不会追踪到软链对应的真实地址,即软链被当作文件
      Parameters:
      path - Path
      Returns:
      如果为目录true
    • isDirectory

      public static boolean isDirectory(File file)
      判断是否为目录,如果file为null,则返回false
      Parameters:
      file - 文件
      Returns:
      如果为目录true
    • isDirectory

      public static boolean isDirectory(Path path, boolean isFollowLinks)
      判断是否为目录,如果file为null,则返回false
      Parameters:
      path - Path
      isFollowLinks - 是否追踪到软链对应的真实地址
      Returns:
      如果为目录true
    • file

      public static File file(String path)
      创建File对象,自动识别相对或绝对路径,相对路径将自动从ClassPath下寻找
      Parameters:
      path - 文件路径
      Returns:
      File
    • file

      public static File file(String... names)
      通过多层目录创建文件

      元素名(多层目录名)

      Parameters:
      names - 文件名
      Returns:
      the file 文件
    • file

      public static File file(URI uri)
      创建File对象
      Parameters:
      uri - 文件URI
      Returns:
      File
    • file

      public static File file(URL url)
      创建File对象
      Parameters:
      url - 文件URL
      Returns:
      File
    • file

      public static File file(String parent, String path)
      创建File对象 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
      Parameters:
      parent - 父目录
      path - 文件路径
      Returns:
      File
    • file

      public static File file(File parent, String path)
      创建File对象 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
      Parameters:
      parent - 父文件对象
      path - 文件路径
      Returns:
      File
    • file

      public static File file(File directory, String... names)
      通过多层目录参数创建文件 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
      Parameters:
      directory - 父目录
      names - 元素名(多层目录名)
      Returns:
      the file 文件
    • loopFiles

      public static List<File> loopFiles(String path)
      递归遍历目录以及子目录中的所有文件
      Parameters:
      path - 当前遍历文件或目录的路径
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(File file)
      递归遍历目录以及子目录中的所有文件
      Parameters:
      file - 当前遍历文件
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(String path, FileFilter fileFilter)
      递归遍历目录以及子目录中的所有文件 如果提供file为文件,直接返回过滤结果
      Parameters:
      path - 当前遍历文件或目录的路径
      fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(File file, FileFilter fileFilter)
      递归遍历目录以及子目录中的所有文件 如果提供file为文件,直接返回过滤结果
      Parameters:
      file - 当前遍历文件或目录
      fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(Path path, FileFilter fileFilter)
      递归遍历目录以及子目录中的所有文件 如果提供path为文件,直接返回过滤结果
      Parameters:
      path - 当前遍历文件或目录
      fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录,null表示接收全部文件
      Returns:
      文件列表
    • loopFiles

      public static List<File> loopFiles(Path path, int maxDepth, FileFilter fileFilter)
      递归遍历目录以及子目录中的所有文件 如果提供file为文件,直接返回过滤结果
      Parameters:
      path - 当前遍历文件或目录
      maxDepth - 遍历最大深度,-1表示遍历到没有目录为止
      fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录,null表示接收全部文件
      Returns:
      文件列表
    • walkFiles

      public static void walkFiles(Path start, FileVisitor<? super Path> visitor)
      遍历指定path下的文件并做处理
      Parameters:
      start - 起始路径,必须为目录
      visitor - FileVisitor 接口,用于自定义在访问文件时,访问目录前后等节点做的操作
      See Also:
    • walkFiles

      public static void walkFiles(File file, Consumer<File> consumer)
      递归遍历目录并处理目录下的文件,可以处理目录或文件:
      • 非目录则直接调用Consumer处理
      • 目录则递归调用此方法处理
      Parameters:
      file - 文件或目录,文件直接处理
      consumer - 文件处理器,只会处理文件
    • walkFiles

      public static void walkFiles(Path start, int maxDepth, FileVisitor<? super Path> visitor)
      遍历指定path下的文件并做处理
      Parameters:
      start - 起始路径,必须为目录
      maxDepth - 最大遍历深度,-1表示不限制深度
      visitor - FileVisitor 接口,用于自定义在访问文件时,访问目录前后等节点做的操作
      See Also:
    • exists

      public static boolean exists(String path)
      判断文件是否存在,如果path为null,则返回false
      Parameters:
      path - 文件路径
      Returns:
      如果存在返回true
    • exists

      public static boolean exists(File file)
      判断文件是否存在,如果file为null,则返回false
      Parameters:
      file - 文件
      Returns:
      如果存在返回true
    • exists

      public static boolean exists(String directory, String regexp)
      是否存在匹配文件
      Parameters:
      directory - 文件夹路径
      regexp - 文件夹中所包含文件名的正则表达式
      Returns:
      如果存在匹配文件返回true
    • exists

      public static boolean exists(Path path, boolean isFollowLinks)
      判断文件或目录是否存在
      Parameters:
      path - 文件
      isFollowLinks - 是否跟踪软链(快捷方式)
      Returns:
      是否存在
    • newerThan

      public static boolean newerThan(File file, File reference)
      给定文件或目录的最后修改时间是否晚于给定时间
      Parameters:
      file - 文件或目录
      reference - 参照文件
      Returns:
      是否晚于给定时间
    • newerThan

      public static boolean newerThan(File file, long timeMillis)
      给定文件或目录的最后修改时间是否晚于给定时间
      Parameters:
      file - 文件或目录
      timeMillis - 做为对比的时间
      Returns:
      是否晚于给定时间
    • touch

      public static File touch(String path) throws InternalException
      创建文件及其父目录,如果这个文件存在,直接返回这个文件 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      path - 文件的全路径,使用POSIX风格
      Returns:
      文件, 若路径为null, 返回null
      Throws:
      InternalException - 异常
    • touch

      public static File touch(File file) throws InternalException
      创建文件及其父目录,如果这个文件存在,直接返回这个文件 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      file - 文件对象
      Returns:
      文件, 若路径为null, 返回null
      Throws:
      InternalException - 异常
    • touch

      public static File touch(File parent, String path) throws InternalException
      创建文件及其父目录,如果这个文件存在,直接返回这个文件 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      parent - 父文件对象
      path - 文件路径
      Returns:
      File
      Throws:
      InternalException - 异常
    • touch

      public static File touch(String parent, String path) throws InternalException
      创建文件及其父目录,如果这个文件存在,直接返回这个文件 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      parent - 父文件对象
      path - 文件路径
      Returns:
      File
      Throws:
      InternalException - 异常
    • delete

      public static boolean delete(String fullFileOrDirPath) throws InternalException
      删除文件或者文件夹 路径如果为相对路径,会转换为ClassPath路径! 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      fullFileOrDirPath - 文件或者目录的路径
      Returns:
      成功与否
      Throws:
      InternalException - IO异常
    • delete

      public static boolean delete(File file) throws InternalException
      删除文件或者文件夹 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      file - 文件对象
      Returns:
      成功与否
      Throws:
      InternalException - IO异常
    • delete

      public static boolean delete(Path path) throws InternalException
      删除文件或者文件夹 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      path - 文件对象
      Returns:
      成功与否
      Throws:
      InternalException - IO异常
    • clean

      public static boolean clean(String dirPath) throws InternalException
      清空文件夹 注意:清空文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      dirPath - 文件夹路径
      Returns:
      成功与否
      Throws:
      InternalException - 异常
    • clean

      public static boolean clean(File directory) throws InternalException
      清空文件夹 注意:清空文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      directory - 文件夹
      Returns:
      成功与否
      Throws:
      InternalException - 异常
    • mkdir

      public static File mkdir(String dirPath)
      创建文件夹,如果存在直接返回此文件夹 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      dirPath - 文件夹路径,使用POSIX格式,无论哪个平台
      Returns:
      创建的目录
    • mkdir

      public static File mkdir(File dir)
      创建文件夹,会递归自动创建其不存在的父文件夹,如果存在直接返回此文件夹 此方法不对File对象类型做判断,如果File不存在,无法判断其类型
      Parameters:
      dir - 目录
      Returns:
      创建的目录
    • mkdir

      public static Path mkdir(Path dir)
      创建所给目录及其父目录
      Parameters:
      dir - 目录
      Returns:
      目录
    • mkParentDirs

      public static File mkParentDirs(File file)
      创建所给文件或目录的父目录
      Parameters:
      file - 文件或目录
      Returns:
      父目录
    • mkParentDirs

      public static File mkParentDirs(String path)
      创建父文件夹,如果存在直接返回此文件夹
      Parameters:
      path - 文件夹路径,使用POSIX格式,无论哪个平台
      Returns:
      创建的目录
    • mkdirsSafely

      public static boolean mkdirsSafely(File dir, int tryCount, long sleepMillis)
      安全地级联创建目录 (确保并发环境下能创建成功)
           并发环境下,假设 test 目录不存在,如果线程A mkdirs "test/A" 目录,线程B mkdirs "test/B"目录,
           其中一个线程可能会失败,进而导致以下代码抛出 FileNotFoundException 异常
      
           file.getParentFile().mkdirs(); // 父目录正在被另一个线程创建中,返回 false
           file.createNewFile(); // 抛出 IO 异常,因为该线程无法感知到父目录已被创建
       
      Parameters:
      dir - 待创建的目录
      tryCount - 最大尝试次数
      sleepMillis - 线程等待的毫秒数
      Returns:
      true表示创建成功,false表示创建失败
    • createTempFile

      public static File createTempFile(File dir) throws InternalException
      创建临时文件 创建后的文件名为 prefix[Randon].tmp
      Parameters:
      dir - 临时文件创建的所在目录
      Returns:
      临时文件
      Throws:
      InternalException - 异常
    • createTempFile

      public static File createTempFile() throws InternalException
      在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Randon].tmp 默认临时文件目录由系统属性 java.io.tmpdir 指定 在 UNIX 系统上,此属性的默认值通常是 "tmp""vartmp" 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
      Returns:
      临时文件
      Throws:
      InternalException - IO异常
    • createTempFile

      public static File createTempFile(String suffix, boolean isReCreat) throws InternalException
      在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Randon].suffix 默认临时文件目录由系统属性 java.io.tmpdir 指定 在 UNIX 系统上,此属性的默认值通常是 "tmp""vartmp" 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
      Parameters:
      suffix - 后缀,如果null则使用默认.tmp
      isReCreat - 是否重新创建文件(删掉原来的,创建新的)
      Returns:
      临时文件
      Throws:
      InternalException - IO异常
    • createTempFile

      public static File createTempFile(String prefix, String suffix, boolean isReCreat) throws InternalException
      在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Randon].suffix 默认临时文件目录由系统属性 java.io.tmpdir 指定 在 UNIX 系统上,此属性的默认值通常是 "tmp""vartmp" 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响
      Parameters:
      prefix - 前缀,至少3个字符
      suffix - 后缀,如果null则使用默认.tmp
      isReCreat - 是否重新创建文件(删掉原来的,创建新的)
      Returns:
      临时文件
      Throws:
      InternalException - IO异常
    • createTempFile

      public static File createTempFile(File dir, boolean isReCreat) throws InternalException
      创建临时文件 创建后的文件名为 prefix[Randon].tmp
      Parameters:
      dir - 临时文件创建的所在目录
      isReCreat - 是否重新创建文件(删掉原来的,创建新的)
      Returns:
      临时文件
      Throws:
      InternalException - 异常
    • createTempFile

      public static File createTempFile(String prefix, String suffix, File dir, boolean isReCreat) throws InternalException
      创建临时文件 创建后的文件名为 prefix[Randon].suffix
      Parameters:
      prefix - 前缀,至少3个字符
      suffix - 后缀,如果null则使用默认.tmp
      dir - 临时文件创建的所在目录
      isReCreat - 是否重新创建文件(删掉原来的,创建新的)
      Returns:
      临时文件
      Throws:
      InternalException - 异常
    • copyFile

      public static File copyFile(String src, String dest, StandardCopyOption... options) throws InternalException
      通过JDK7+的 Files.copy(Path, Path, CopyOption...) 方法拷贝文件
      Parameters:
      src - 源文件路径
      dest - 目标文件或目录路径,如果为目录使用与源文件相同的文件名
      options - StandardCopyOption
      Returns:
      File
      Throws:
      InternalException - 异常
    • copyFile

      public static File copyFile(File src, File dest, StandardCopyOption... options) throws InternalException
      通过JDK7+的 Files.copy(Path, Path, CopyOption...) 方法拷贝文件
      Parameters:
      src - 源文件
      dest - 目标文件或目录,如果为目录使用与源文件相同的文件名
      options - StandardCopyOption
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • copyFile

      public static Path copyFile(Path src, Path dest, StandardCopyOption... options) throws InternalException
      通过JDK7+的 Files.copy(Path, Path, CopyOption...) 方法拷贝文件
      Parameters:
      src - 源文件路径
      dest - 目标文件或目录,如果为目录使用与源文件相同的文件名
      options - StandardCopyOption
      Returns:
      Path
      Throws:
      InternalException - 异常
    • copyFile

      public static long copyFile(File input, OutputStream output) throws IOException
      将字节从File复制到OutputStream 这个方法在内部缓冲输入,所以不需要使用 BufferedInputStream
      Parameters:
      input - 要读取的 File
      output - 要写入的输出流 OutputStream
      Returns:
      复制的字节数
      Throws:
      NullPointerException - 如果输入或输出为空
      IOException - 如果发生I/O错误
    • copyFile

      public static File copyFile(File src, File dest, boolean isOverride) throws InternalException
      复制文件或目录 情况如下:
       1、src和dest都为目录,则将src下所有文件(包括子目录)拷贝到dest下
       2、src和dest都为文件,直接复制,名字为dest
       3、src为文件,dest为目录,将src拷贝到dest目录下
       
      Parameters:
      src - 源文件
      dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
      isOverride - 是否覆盖目标文件
      Returns:
      目标目录或文件
      Throws:
      InternalException - 异常
    • copy

      public static File copy(String srcPath, String destPath, boolean isOverride) throws InternalException
      复制文件或目录 如果目标文件为目录,则将源文件以相同文件名拷贝到目标目录
      Parameters:
      srcPath - 源文件或目录
      destPath - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
      isOverride - 是否覆盖目标文件
      Returns:
      目标目录或文件
      Throws:
      InternalException - 异常
    • copy

      public static File copy(File src, File dest, boolean isOverride) throws InternalException
      复制文件或目录 情况如下:
       1、src和dest都为目录,则将src目录及其目录下所有文件目录拷贝到dest下
       2、src和dest都为文件,直接复制,名字为dest
       3、src为文件,dest为目录,将src拷贝到dest目录下
       
      Parameters:
      src - 源文件
      dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
      isOverride - 是否覆盖目标文件
      Returns:
      目标目录或文件
      Throws:
      InternalException - 异常
    • copyContent

      public static File copyContent(File src, File dest, boolean isOverride) throws InternalException
      复制文件或目录 情况如下:
       1、src和dest都为目录,则将src下所有文件目录拷贝到dest下
       2、src和dest都为文件,直接复制,名字为dest
       3、src为文件,dest为目录,将src拷贝到dest目录下
       
      Parameters:
      src - 源文件
      dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
      isOverride - 是否覆盖目标文件
      Returns:
      目标目录或文件
      Throws:
      InternalException - 异常
    • rename

      public static File rename(File file, String newName, boolean isOverride)
      修改文件或目录的文件名,不变更路径,只是简单修改文件名,不保留扩展名
       FileKit.rename(file, "aaa.png", true) xx/xx.png =》xx/aaa.png
       
      Parameters:
      file - 被修改的文件
      newName - 新的文件名,如需扩展名,需自行在此参数加上,原文件名的扩展名不会被保留
      isOverride - 是否覆盖目标文件
      Returns:
      目标文件
    • rename

      public static Path rename(Path path, String newName, boolean isOverride)
      修改文件或目录的文件名,不变更路径,只是简单修改文件名
       FileKit.rename(file, "aaa.jpg", false) xx/xx.png =》xx/aaa.jpg
       
      Parameters:
      path - 被修改的文件
      newName - 新的文件名,包括扩展名
      isOverride - 是否覆盖目标文件
      Returns:
      目标文件Path
    • rename

      public static File rename(File file, String newName, boolean isRetainExt, boolean isOverride)
      修改文件或目录的文件名,不变更路径,只是简单修改文件名 重命名有两种模式: 1、isRetainExt为true时,保留原扩展名:
       FileKit.rename(file, "aaa", true) xx/xx.png =》xx/aaa.png
       

      2、isRetainExt为false时,不保留原扩展名,需要在newName中

       FileKit.rename(file, "aaa.jpg", false) xx/xx.png =》xx/aaa.jpg
       
      Parameters:
      file - 被修改的文件
      newName - 新的文件名,包括扩展名
      isRetainExt - 是否保留原文件的扩展名,如果保留,则newName不需要加扩展名
      isOverride - 是否覆盖目标文件
      Returns:
      目标文件
    • move

      public static void move(File src, File target, boolean isOverride) throws InternalException
      移动文件或者目录
      Parameters:
      src - 源文件或者目录
      target - 目标文件或者目录
      isOverride - 是否覆盖目标,只有目标为文件才覆盖
      Throws:
      InternalException - IO异常
    • move

      public static Path move(Path src, Path target, boolean isOverride)
      移动文件或目录 当目标是目录时,会将源文件或文件夹整体移动至目标目录下
      Parameters:
      src - 源文件或目录路径
      target - 目标路径,如果为目录,则移动到此目录下
      isOverride - 是否覆盖目标文件
      Returns:
      目标文件Path
    • getCanonicalPath

      public static String getCanonicalPath(File file)
      获取规范的绝对路径
      Parameters:
      file - 文件
      Returns:
      规范绝对路径, 如果传入file为null, 返回null
    • getAbsolutePath

      public static String getAbsolutePath(String path)
      获取绝对路径,相对于ClassPath的目录 如果给定就是绝对路径,则返回原路径,原路径把所有\替换为/ 兼容Spring风格的路径表示,例如:classpath:config/example.setting也会被识别后转换
      Parameters:
      path - 相对路径
      Returns:
      绝对路径
    • getAbsolutePath

      public static String getAbsolutePath(String path, Class<?> baseClass)
      获取绝对路径 此方法不会判定给定路径是否有效(文件或目录存在)
      Parameters:
      path - 相对路径
      baseClass - 相对路径所相对的类
      Returns:
      绝对路径
    • getAbsolutePath

      public static String getAbsolutePath(File file)
      获取标准的绝对路径
      Parameters:
      file - 文件
      Returns:
      绝对路径
    • isAbsolutePath

      public static boolean isAbsolutePath(String path)
      给定路径已经是绝对路径 此方法并没有针对路径做标准化,建议先执行normalize(String)方法标准化路径后判断 绝对路径判断条件是:
      • 以/开头的路径
      • 满足类似于 c:/xxxxx,其中祖母随意,不区分大小写
      • 满足类似于 d:\xxxxx,其中祖母随意,不区分大小写
      Parameters:
      path - 需要检查的Path
      Returns:
      是否已经是绝对路径
    • equals

      public static boolean equals(File file1, File file2) throws InternalException
      检查两个文件是否是同一个文件 所谓文件相同,是指File对象是否指向同一个文件或文件夹
      Parameters:
      file1 - 文件1
      file2 - 文件2
      Returns:
      是否相同
      Throws:
      InternalException - 异常
      See Also:
    • readFile

      public static String readFile(File file)
      读取文件
      Parameters:
      file - 文件
      Returns:
      内容
    • listFileNames

      public static List<String> listFileNames(String path) throws InternalException
      获得指定目录下所有文件 不会扫描子目录
      Parameters:
      path - 相对ClassPath的目录或者绝对路径目录
      Returns:
      文件路径列表(如果是jar中的文件, 则给定类似.jar ! / xxx / xxx的路径)
      Throws:
      InternalException - 异常
    • newFile

      public static File newFile(String path)
      创建File对象,相当于调用new File(),不做任何处理
      Parameters:
      path - 文件路径
      Returns:
      File
    • getTmpDirPath

      public static String getTmpDirPath()
      获取临时文件路径(绝对路径)
      Returns:
      临时文件路径
    • getTmpDir

      public static File getTmpDir()
      获取临时文件目录
      Returns:
      临时文件目录
    • getUserHomePath

      public static String getUserHomePath()
      获取用户路径(绝对路径)
      Returns:
      用户路径
    • getUserHomeDir

      public static File getUserHomeDir()
      获取用户目录
      Returns:
      用户目录
    • indexOfSuffix

      public static int indexOfSuffix(String filename)
      返回最后一个扩展分隔符的索引点.
      Parameters:
      filename - 查找最后一个路径分隔符的文件名
      Returns:
      最后一个分隔符字符的索引,如果没有这样的字符,则为-1
    • indexOfLastSeparator

      public static int indexOfLastSeparator(String filename)
      返回最后一个目录分隔符的索引

      此方法将处理Unix或Windows格式的文件 返回最后一个正斜杠或反斜杠的位置.

      Parameters:
      filename - 查找最后一个路径分隔符的文件名
      Returns:
      最后一个分隔符字符的索引,如果没有这样的字符,则为-1
    • lastModifiedTime

      public static Date lastModifiedTime(File file)
      指定文件最后修改时间
      Parameters:
      file - 文件
      Returns:
      最后修改时间
    • lastModifiedTime

      public static Date lastModifiedTime(String path)
      指定路径文件最后修改时间
      Parameters:
      path - 绝对路径
      Returns:
      最后修改时间
    • size

      public static long size(File file)
      计算目录或文件的总大小 当给定对象为文件时,直接调用 File.length() 当给定对象为目录时,遍历目录下的所有文件和目录,递归计算其大小,求和返回 此方法不包括目录本身的占用空间大小
      Parameters:
      file - 目录或文件,null或者文件不存在返回0
      Returns:
      总大小,bytes长度
    • size

      public static long size(File file, boolean includeDirSize)
      计算目录或文件的总大小 当给定对象为文件时,直接调用 File.length() 当给定对象为目录时,遍历目录下的所有文件和目录,递归计算其大小,求和返回
      Parameters:
      file - 目录或文件
      includeDirSize - 是否包括每层目录本身的大小
      Returns:
      总大小, bytes长度
    • isSymlink

      public static boolean isSymlink(File file)
      判断是否为符号链接文件
      Parameters:
      file - 被检查的文件
      Returns:
      是否为符号链接文件
    • contentEquals

      public static boolean contentEquals(File file1, File file2) throws InternalException
      比较两个文件内容是否相同 首先比较长度,长度一致再比较内容 此方法来自Apache Commons io
      Parameters:
      file1 - 文件1
      file2 - 文件2
      Returns:
      两个文件内容一致返回true, 否则false
      Throws:
      InternalException - 异常
    • contentEqualsIgnoreEOL

      public static boolean contentEqualsIgnoreEOL(File file1, File file2, Charset charset) throws InternalException
      比较两个文件内容是否相同 首先比较长度,长度一致再比较内容,比较内容采用按行读取,每行比较 此方法来自Apache Commons io
      Parameters:
      file1 - 文件1
      file2 - 文件2
      charset - 编码,null表示使用平台默认编码 两个文件内容一致返回true,否则false
      Returns:
      the boolean
      Throws:
      InternalException - 异常
    • pathEquals

      public static boolean pathEquals(File file1, File file2)
      文件路径是否相同 取两个文件的绝对路径比较,在Windows下忽略大小写,在Linux下不忽略
      Parameters:
      file1 - 文件1
      file2 - 文件2
      Returns:
      文件路径是否相同
    • lastIndexOfSeparator

      public static int lastIndexOfSeparator(String filePath)
      获得最后一个文件路径分隔符的位置
      Parameters:
      filePath - 文件路径
      Returns:
      最后一个文件路径分隔符的位置
    • isModified

      public static boolean isModified(File file, long lastModifyTime)
      判断文件是否被改动 如果文件对象为 null 或者文件不存在,被视为改动
      Parameters:
      file - 文件对象
      lastModifyTime - 上次的改动时间
      Returns:
      是否被改动
    • normalize

      public static String normalize(String path)
      修复路径 如果原路径尾部有分隔符,则保留为标准分隔符(/),否则不保留
      1. 1. 统一用 /
      2. 2. 多个 / 转换为一个 /
      3. 3. 去除左边空格
      4. 4. .. 和 . 转换为绝对路径,当..多于已有路径时,直接返回根路径

      栗子:

       "/foo//" =  "/foo/"
       "/foo/./" =  "/foo/"
       "/foo/../bar" =  "/bar"
       "/foo/../bar/" =  "/bar/"
       "/foo/../bar/../baz" =  "/baz"
       "foo/bar/.." =  "foo"
       "foo/../bar" =  "bar"
       "foo/../../bar" =  "bar"
       "//server/foo/../bar" =  "/server/bar"
       "//server/../bar" =  "/bar"
       "~/foo/../bar/" =  "~/bar/"
       "~/../bar" =》 普通用户运行是'bar的home目录',ROOT用户运行是'/bar'
       
      Parameters:
      path - 原路径
      Returns:
      修复后的路径
    • subPath

      public static String subPath(String rootDir, File file)
      获得相对子路径

      栗子:

       dirPath: /data/aaa/bbb    filePath: /data/aaa/bbb/ccc     =     ccc
       dirPath: /data/Aaa/bbb    filePath: /data/aaa/bbb/ccc.txt     =     ccc.txt
       
      Parameters:
      rootDir - 绝对父路径
      file - 文件
      Returns:
      相对子路径
    • subPath

      public static String subPath(String dirPath, String filePath)
      获得相对子路径,忽略大小写

      栗子:

       dirPath: /data/aaa/bbb    filePath: /data/aaa/bbb/ccc     =     ccc
       dirPath: /data/Aaa/bbb    filePath: /data/aaa/bbb/ccc.txt     =     ccc.txt
       dirPath: /data/Aaa/bbb    filePath: /data/aaa/bbb/     =     ""
       
      Parameters:
      dirPath - 父路径
      filePath - 文件路径
      Returns:
      相对子路径
    • getPathEle

      public static Path getPathEle(Path path, int index)
      获取指定位置的子路径部分,支持负数,例如index为-1表示从后数第一个节点位置
      Parameters:
      path - 路径
      index - 路径节点位置,支持负数(负数从后向前计数)
      Returns:
      获取的子路径
    • getLastPathEle

      public static Path getLastPathEle(Path path)
      获取指定位置的最后一个子路径部分
      Parameters:
      path - 路径
      Returns:
      获取的最后一个子路径
    • subPath

      public static Path subPath(Path path, int fromIndex, int toIndex)
      获取指定位置的子路径部分,支持负数,例如起始为-1表示从后数第一个节点位置
      Parameters:
      path - 路径
      fromIndex - 起始路径节点(包括)
      toIndex - 结束路径节点(不包括)
      Returns:
      获取的子路径
    • getName

      public static String getName(File file)
      返回文件名
      Parameters:
      file - 文件
      Returns:
      文件名
    • getName

      public static String getName(String filePath)
      返回文件名
      Parameters:
      filePath - 文件
      Returns:
      文件名
    • getName

      public static String getName(Path path)
      获取Path文件名
      Parameters:
      path - Path
      Returns:
      文件名
    • getSuffix

      public static String getSuffix(File file)
      获取文件后缀名,扩展名不带“.”
      Parameters:
      file - 文件
      Returns:
      扩展名
    • getSuffix

      public static String getSuffix(String fileName)
      获得文件后缀名,扩展名不带“.”
      Parameters:
      fileName - 文件名
      Returns:
      扩展名
    • getPrefix

      public static String getPrefix(File file)
      返回主文件名
      Parameters:
      file - 文件
      Returns:
      主文件名
    • getPrefix

      public static String getPrefix(String fileName)
      返回主文件名
      Parameters:
      fileName - 完整文件名
      Returns:
      主文件名
    • endsWith

      public static boolean endsWith(File file, String suffix)
      判断文件路径是否有指定后缀,忽略大小写 常用语判断扩展名
      Parameters:
      file - 文件或目录
      suffix - 后缀
      Returns:
      是否有指定后缀
    • getType

      public static String getType(File file) throws InternalException
      根据文件流的头部信息获得文件类型
      Parameters:
      file - 文件 File
      Returns:
      类型, 文件的扩展名, 未找到为null
      Throws:
      InternalException - 异常
      See Also:
    • getAttributes

      public static BasicFileAttributes getAttributes(Path path, boolean isFollowLinks) throws InternalException
      获取文件属性
      Parameters:
      path - 文件路径Path
      isFollowLinks - 是否跟踪到软链对应的真实路径
      Returns:
      BasicFileAttributes
      Throws:
      InternalException - 异常
    • getInputStream

      public static BufferedInputStream getInputStream(Path path) throws InternalException
      获得输入流
      Parameters:
      path - Path
      Returns:
      输入流
      Throws:
      InternalException - 文件未找到
    • getInputStream

      public static BufferedInputStream getInputStream(File file) throws InternalException
      获得输入流
      Parameters:
      file - 文件
      Returns:
      输入流
      Throws:
      InternalException - 文件未找到
    • getInputStream

      public static BufferedInputStream getInputStream(String path) throws InternalException
      获得输入流
      Parameters:
      path - 文件路径
      Returns:
      输入流
      Throws:
      InternalException - 文件未找到
    • getBOMInputStream

      public static BOMInputStream getBOMInputStream(File file) throws InternalException
      获得BOM输入流,用于处理带BOM头的文件
      Parameters:
      file - 文件
      Returns:
      输入流
      Throws:
      InternalException - 文件未找到
    • getBOMReader

      public static BufferedReader getBOMReader(File file)
      读取带BOM头的文件为Reader
      Parameters:
      file - 文件
      Returns:
      BufferedReader对象
    • getReader

      public static BufferedReader getReader(Path path) throws InternalException
      获得一个文件读取器
      Parameters:
      path - 文件Path
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(File file) throws InternalException
      获得一个文件读取器
      Parameters:
      file - 文件
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(String path) throws InternalException
      获得一个文件读取器
      Parameters:
      path - 文件路径
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(Path path, Charset charset) throws InternalException
      获得一个文件读取器
      Parameters:
      path - 文件Path
      charset - 字符集
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(File file, String charsetName) throws InternalException
      获得一个文件读取器
      Parameters:
      file - 文件
      charsetName - 字符集
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(File file, Charset charset) throws InternalException
      获得一个文件读取器
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(String path, String charsetName) throws InternalException
      获得一个文件读取器
      Parameters:
      path - 绝对路径
      charsetName - 字符集
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getReader

      public static BufferedReader getReader(String path, Charset charset) throws InternalException
      获得一个文件读取器
      Parameters:
      path - 绝对路径
      charset - 字符集
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • readBytes

      public static byte[] readBytes(File file) throws InternalException
      读取文件所有数据 文件的长度不能超过Integer.MAX_VALUE
      Parameters:
      file - 文件
      Returns:
      字节码
      Throws:
      InternalException - 异常
    • readBytes

      public static byte[] readBytes(String filePath) throws InternalException
      读取文件所有数据 文件的长度不能超过Integer.MAX_VALUE
      Parameters:
      filePath - 文件路径
      Returns:
      字节码
      Throws:
      InternalException - 异常
    • readString

      public static String readString(File file) throws InternalException
      读取文件内容
      Parameters:
      file - 文件
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(String path) throws InternalException
      读取文件内容
      Parameters:
      path - 文件路径
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(File file, String charsetName) throws InternalException
      读取文件内容
      Parameters:
      file - 文件
      charsetName - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(File file, Charset charset) throws InternalException
      读取文件内容
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(String path, String charsetName) throws InternalException
      读取文件内容
      Parameters:
      path - 文件路径
      charsetName - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(String path, Charset charset) throws InternalException
      读取文件内容
      Parameters:
      path - 文件路径
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readString

      public static String readString(URL url, String charset) throws InternalException
      读取文件内容
      Parameters:
      url - 文件URL
      charset - 字符集
      Returns:
      内容
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(String path, T collection) throws InternalException
      从文件中读取每一行的UTF-8编码数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件路径
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(String path, String charset, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件路径
      charset - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(String path, Charset charset, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件路径
      charset - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(File file, T collection) throws InternalException
      从文件中读取每一行数据,数据编码为UTF-8
      Type Parameters:
      T - 集合类型
      Parameters:
      file - 文件路径
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(File file, String charset, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      file - 文件路径
      charset - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(File file, Charset charset, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      file - 文件路径
      charset - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(URL url, T collection) throws InternalException
      从文件中读取每一行数据,编码为UTF-8
      Type Parameters:
      T - 集合类型
      Parameters:
      url - 文件的URL
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(URL url, String charsetName, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      url - 文件的URL
      charsetName - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static <T extends Collection<String>> T readLines(URL url, Charset charset, T collection) throws InternalException
      从文件中读取每一行数据
      Type Parameters:
      T - 集合类型
      Parameters:
      url - 文件的URL
      charset - 字符集
      collection - 集合
      Returns:
      文件中的每行内容的集合
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(URL url) throws InternalException
      从文件中读取每一行数据
      Parameters:
      url - 文件的URL
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(URL url, String charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      url - 文件的URL
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(URL url, Charset charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      url - 文件的URL
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(String path) throws InternalException
      从文件中读取每一行数据,编码为UTF-8
      Parameters:
      path - 文件路径
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(String path, String charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      path - 文件路径
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(String path, Charset charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      path - 文件路径
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(File file) throws InternalException
      从文件中读取每一行数据
      Parameters:
      file - 文件
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(File file, String charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static List<String> readLines(File file, Charset charset) throws InternalException
      从文件中读取每一行数据
      Parameters:
      file - 文件
      charset - 字符集
      Returns:
      文件中的每行内容的集合List
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(File file, XConsumer<String> lineHandler) throws InternalException
      按行处理文件内容,编码为UTF-8
      Parameters:
      file - 文件
      lineHandler - LineHandler行处理器
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(File file, Charset charset, XConsumer<String> lineHandler) throws InternalException
      按行处理文件内容
      Parameters:
      file - 文件
      charset - 编码
      lineHandler - LineHandler行处理器
      Throws:
      InternalException - 异常
    • readLines

      public static void readLines(RandomAccessFile file, Charset charset, XConsumer<String> lineHandler)
      按行处理文件内容
      Parameters:
      file - RandomAccessFile文件
      charset - 编码
      lineHandler - LineHandler行处理器
      Throws:
      InternalException - 异常
    • readLine

      public static void readLine(RandomAccessFile file, Charset charset, XConsumer<String> lineHandler)
      单行处理文件内容
      Parameters:
      file - RandomAccessFile文件
      charset - 编码
      lineHandler - LineHandler行处理器
    • readLine

      public static String readLine(RandomAccessFile file, Charset charset)
      单行处理文件内容
      Parameters:
      file - RandomAccessFile文件
      charset - 编码
      Returns:
      行内容
    • load

      public static <T> T load(String path, FileReader.ReaderHandler<T> readerHandler) throws InternalException
      按照给定的readerHandler读取文件中的数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件的绝对路径
      readerHandler - Reader处理类
      Returns:
      从文件中load出的数据
      Throws:
      InternalException - 异常
    • load

      public static <T> T load(String path, String charset, FileReader.ReaderHandler<T> readerHandler) throws InternalException
      按照给定的readerHandler读取文件中的数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件的绝对路径
      charset - 字符集
      readerHandler - Reader处理类
      Returns:
      从文件中load出的数据
      Throws:
      InternalException - 异常
    • load

      public static <T> T load(String path, Charset charset, FileReader.ReaderHandler<T> readerHandler) throws InternalException
      按照给定的readerHandler读取文件中的数据
      Type Parameters:
      T - 集合类型
      Parameters:
      path - 文件的绝对路径
      charset - 字符集
      readerHandler - Reader处理类
      Returns:
      从文件中load出的数据
      Throws:
      InternalException - 异常
    • load

      public static <T> T load(File file, FileReader.ReaderHandler<T> readerHandler) throws InternalException
      按照给定的readerHandler读取文件中的数据
      Type Parameters:
      T - 集合类型
      Parameters:
      file - 文件
      readerHandler - Reader处理类
      Returns:
      从文件中load出的数据
      Throws:
      InternalException - 异常
    • load

      public static <T> T load(File file, Charset charset, FileReader.ReaderHandler<T> readerHandler) throws InternalException
      按照给定的readerHandler读取文件中的数据
      Type Parameters:
      T - 集合类型
      Parameters:
      file - 文件
      charset - 字符集
      readerHandler - Reader处理类
      Returns:
      从文件中load出的数据
      Throws:
      InternalException - 异常
    • getOutputStream

      public static BufferedOutputStream getOutputStream(File file) throws InternalException
      获得一个输出流对象
      Parameters:
      file - 文件
      Returns:
      输出流对象
      Throws:
      InternalException - 异常
    • getOutputStream

      public static BufferedOutputStream getOutputStream(String path) throws InternalException
      获得一个输出流对象
      Parameters:
      path - 输出到的文件路径,绝对路径
      Returns:
      输出流对象
      Throws:
      InternalException - 异常
    • getWriter

      public static BufferedWriter getWriter(String path, String charsetName, boolean isAppend) throws InternalException
      获得一个带缓存的写入对象
      Parameters:
      path - 输出路径,绝对路径
      charsetName - 字符集
      isAppend - 是否追加
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getWriter

      public static BufferedWriter getWriter(String path, Charset charset, boolean isAppend) throws InternalException
      获得一个带缓存的写入对象
      Parameters:
      path - 输出路径,绝对路径
      charset - 字符集
      isAppend - 是否追加
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getWriter

      public static BufferedWriter getWriter(File file, String charsetName, boolean isAppend) throws InternalException
      获得一个带缓存的写入对象
      Parameters:
      file - 输出文件
      charsetName - 字符集
      isAppend - 是否追加
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getWriter

      public static BufferedWriter getWriter(File file, Charset charset, boolean isAppend) throws InternalException
      获得一个带缓存的写入对象
      Parameters:
      file - 输出文件
      charset - 字符集
      isAppend - 是否追加
      Returns:
      BufferedReader对象
      Throws:
      InternalException - 异常
    • getPrintWriter

      public static PrintWriter getPrintWriter(String path, String charset, boolean isAppend) throws InternalException
      获得一个打印写入对象,可以有print
      Parameters:
      path - 输出路径,绝对路径
      charset - 字符集
      isAppend - 是否追加
      Returns:
      打印对象
      Throws:
      InternalException - 异常
    • getPrintWriter

      public static PrintWriter getPrintWriter(String path, Charset charset, boolean isAppend) throws InternalException
      获得一个打印写入对象,可以有print
      Parameters:
      path - 输出路径,绝对路径
      charset - 字符集
      isAppend - 是否追加
      Returns:
      打印对象
      Throws:
      InternalException - 异常
    • getPrintWriter

      public static PrintWriter getPrintWriter(File file, String charset, boolean isAppend) throws InternalException
      获得一个打印写入对象,可以有print
      Parameters:
      file - 文件
      charset - 字符集
      isAppend - 是否追加
      Returns:
      打印对象
      Throws:
      InternalException - 异常
    • getPrintWriter

      public static PrintWriter getPrintWriter(File file, Charset charset, boolean isAppend) throws InternalException
      获得一个打印写入对象,可以有print
      Parameters:
      file - 文件
      charset - 字符集
      isAppend - 是否追加
      Returns:
      打印对象
      Throws:
      InternalException - 异常
    • getLineSeparator

      public static String getLineSeparator()
      获取当前系统的换行分隔符
       Windows: \r\n
       Mac: \r
       Linux: \n
       
      Returns:
      换行符
    • writeString

      public static File writeString(String content, String path) throws InternalException
      将String写入文件,覆盖模式,字符集为UTF-8
      Parameters:
      content - 写入的内容
      path - 文件路径
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • writeString

      public static File writeString(String content, File file) throws InternalException
      将String写入文件,覆盖模式,字符集为UTF-8
      Parameters:
      content - 写入的内容
      file - 文件
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • writeString

      public static File writeString(String content, String path, String charset) throws InternalException
      将String写入文件,覆盖模式
      Parameters:
      content - 写入的内容
      path - 文件路径
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • writeString

      public static File writeString(String content, String path, Charset charset) throws InternalException
      将String写入文件,覆盖模式
      Parameters:
      content - 写入的内容
      path - 文件路径
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • writeString

      public static File writeString(String content, File file, String charset) throws InternalException
      将String写入文件,覆盖模式
      Parameters:
      content - 写入的内容
      file - 文件
      charset - 字符集
      Returns:
      被写入的文件
      Throws:
      InternalException - 异常
    • writeString

      public static File writeString(String content, File file, Charset charset) throws InternalException
      将String写入文件,覆盖模式
      Parameters:
      content - 写入的内容
      file - 文件
      charset - 字符集
      Returns:
      被写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, String path) throws InternalException
      将String写入文件,UTF-8编码追加模式
      Parameters:
      content - 写入的内容
      path - 文件路径
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, String path, String charset) throws InternalException
      将String写入文件,追加模式
      Parameters:
      content - 写入的内容
      path - 文件路径
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, String path, Charset charset) throws InternalException
      将String写入文件,追加模式
      Parameters:
      content - 写入的内容
      path - 文件路径
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, File file) throws InternalException
      将String写入文件,UTF-8编码追加模式
      Parameters:
      content - 写入的内容
      file - 文件
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, File file, String charset) throws InternalException
      将String写入文件,追加模式
      Parameters:
      content - 写入的内容
      file - 文件
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • appendString

      public static File appendString(String content, File file, Charset charset) throws InternalException
      将String写入文件,追加模式
      Parameters:
      content - 写入的内容
      file - 文件
      charset - 字符集
      Returns:
      写入的文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, String path) throws InternalException
      将列表写入文件,覆盖模式,编码为UTF-8
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 绝对路径
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, File file) throws InternalException
      将列表写入文件,覆盖模式,编码为UTF-8
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 绝对路径
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, String path, String charset) throws InternalException
      将列表写入文件,覆盖模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 绝对路径
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, String path, Charset charset) throws InternalException
      将列表写入文件,覆盖模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 绝对路径
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, File file, String charset) throws InternalException
      将列表写入文件,覆盖模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, File file, Charset charset) throws InternalException
      将列表写入文件,覆盖模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, File file) throws InternalException
      将列表写入文件,追加模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, String path) throws InternalException
      将列表写入文件,追加模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 文件路径
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, String path, String charset) throws InternalException
      将列表写入文件,追加模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 绝对路径
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, File file, String charset) throws InternalException
      将列表写入文件,追加模式,策略为:
      • 当文件为空,从开头追加,尾部不加空行
      • 当有内容,换行追加,尾部不加空行
      • 当有内容,并末尾有空行,依旧换行追加
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, String path, Charset charset) throws InternalException
      将列表写入文件,追加模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 绝对路径
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • appendLines

      public static <T> File appendLines(Collection<T> list, File file, Charset charset) throws InternalException
      将列表写入文件,追加模式
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, String path, String charset, boolean isAppend) throws InternalException
      将列表写入文件
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 文件路径
      charset - 字符集
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, String path, Charset charset, boolean isAppend) throws InternalException
      将列表写入文件
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      path - 文件路径
      charset - 字符集
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, File file, String charset, boolean isAppend) throws InternalException
      将列表写入文件
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeLines

      public static <T> File writeLines(Collection<T> list, File file, Charset charset, boolean isAppend) throws InternalException
      将列表写入文件
      Type Parameters:
      T - 集合元素类型
      Parameters:
      list - 列表
      file - 文件
      charset - 字符集
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeMap

      public static File writeMap(Map<?,?> map, File file, String kvSeparator, boolean isAppend) throws InternalException
      将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
      Parameters:
      map - Map
      file - 文件
      kvSeparator - 键和值之间的分隔符,如果传入null使用默认分隔符" = "
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeMap

      public static File writeMap(Map<?,?> map, File file, Charset charset, String kvSeparator, boolean isAppend) throws InternalException
      将Map写入文件,每个键值对为一行,一行中键与值之间使用kvSeparator分隔
      Parameters:
      map - Map
      file - 文件
      charset - 字符集编码
      kvSeparator - 键和值之间的分隔符,如果传入null使用默认分隔符" = "
      isAppend - 是否追加
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeBytes

      public static File writeBytes(byte[] data, String path) throws InternalException
      写数据到文件中
      Parameters:
      data - 数据
      path - 目标文件
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeBytes

      public static File writeBytes(byte[] data, File dest) throws InternalException
      写数据到文件中
      Parameters:
      data - 数据
      dest - 目标文件
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeBytes

      public static File writeBytes(byte[] data, File dest, int off, int len, boolean isAppend) throws InternalException
      写入数据到文件
      Parameters:
      data - 数据
      dest - 目标文件
      off - 数据开始位置
      len - 数据长度
      isAppend - 是否追加模式
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeFromStream

      public static File writeFromStream(InputStream in, File dest) throws InternalException
      将流的内容写入文件
      Parameters:
      in - 输入流
      dest - 目标文件
      Returns:
      dest
      Throws:
      InternalException - 异常
    • writeFromStream

      public static File writeFromStream(InputStream in, File dest, boolean isCloseIn) throws InternalException
      将流的内容写入文件
      Parameters:
      in - 输入流
      dest - 目标文件
      isCloseIn - 关闭输入流
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeFromStream

      public static File writeFromStream(InputStream in, String path) throws InternalException
      将路径对应文件写入流中,此方法不会关闭输出流
      Parameters:
      in - 输入流
      path - 文件绝对路径
      Returns:
      目标文件
      Throws:
      InternalException - 异常
    • writeToStream

      public static long writeToStream(File file, OutputStream out) throws InternalException
      将文件写入流中,此方法不会关闭输出流
      Parameters:
      file - 文件
      out - 流
      Returns:
      写出的流byte数
      Throws:
      InternalException - 异常
    • writeToStream

      public static long writeToStream(String path, OutputStream out) throws InternalException
      将流的内容写入文件
      Parameters:
      path - 文件绝对路径
      out - 输出流
      Returns:
      写出的流byte数
      Throws:
      InternalException - 异常
    • readableFileSize

      public static String readableFileSize(File file)
      可读的文件大小
      Parameters:
      file - 文件
      Returns:
      大小
    • readableFileSize

      public static String readableFileSize(long size)
      可读的文件大小 参考 http://stackoverflow.com/questions/3263892/format-file-size-as-mb-gb-etc
      Parameters:
      size - Long类型大小
      Returns:
      大小
    • convertCharset

      public static File convertCharset(File file, Charset srcCharset, Charset destCharset)
      转换文件编码 此方法用于转换文件编码,读取的文件实际编码必须与指定的srcCharset编码一致,否则导致乱码
      Parameters:
      file - 文件
      srcCharset - 原文件的编码,必须与文件内容的编码保持一致
      destCharset - 转码后的编码
      Returns:
      被转换编码的文件
    • convertLineSeparator

      public static File convertLineSeparator(File file, Charset charset, LineSeparator lineSeparator)
      转换换行符 将给定文件的换行符转换为指定换行符
      Parameters:
      file - 文件
      charset - 编码
      lineSeparator - 换行符枚举
      Returns:
      被修改的文件
    • cleanInvalid

      public static String cleanInvalid(String fileName)
      清除文件名中的在Windows下不支持的非法字符,包括: \ / : * ? " < > |
      Parameters:
      fileName - 文件名(必须不包括路径,否则路径符将被替换)
      Returns:
      清理后的文件名
    • containsInvalid

      public static boolean containsInvalid(String fileName)
      文件名中是否包含在Windows下不支持的非法字符,包括: \ / : * ? " < > |
      Parameters:
      fileName - 文件名(必须不包括路径,否则路径符将被替换)
      Returns:
      是否包含非法字符
    • checksumCRC32

      public static long checksumCRC32(File file) throws InternalException
      计算文件CRC32校验码
      Parameters:
      file - 文件,不能为目录
      Returns:
      CRC32值
      Throws:
      InternalException - 异常
    • checksum

      public static Checksum checksum(File file, Checksum checksum) throws InternalException
      计算文件校验码
      Parameters:
      file - 文件,不能为目录
      checksum - Checksum
      Returns:
      Checksum
      Throws:
      InternalException - 异常
    • getWebRoot

      public static File getWebRoot()
      获取Web项目下的web root路径 原理是首先获取ClassPath路径,由于在web项目中ClassPath位于 WEB-INF/classes/下,故向上获取两级目录即可
      Returns:
      web root路径
    • getParent

      public static File getParent(File file)
      获取指定文件的父路径
       getParent(file("d:/aaa/bbb/cc/ddd")) -》 "d:/aaa/bbb/cc"
       
      Parameters:
      file - 目录或文件
      Returns:
      路径File,如果不存在返回null
      Since:
      6.0.0
    • getParent

      public static String getParent(String filePath, int level)
      获取指定层级的父路径
       getParent("/data/aaa/bbb/cc/ddd", 0) - "/data/aaa/bbb/cc/ddd"
       getParent("/data/aaa/bbb/cc/ddd", 2) - "/data/aaa/bbb"
       getParent("/data/aaa/bbb/cc/ddd", 4) - "/data/"
       getParent("/data/aaa/bbb/cc/ddd", 5) - null
       
      Parameters:
      filePath - 目录或文件路径
      level - 层级
      Returns:
      路径File, 如果不存在返回null
    • getParent

      public static File getParent(File file, int level)
      获取指定层级的父路径
       getParent(file("/data/aaa/bbb/cc/ddd", 0)) - "/data/aaa/bbb/cc/ddd"
       getParent(file("/data/aaa/bbb/cc/ddd", 2)) - "/data/aaa/bbb"
       getParent(file("/data/aaa/bbb/cc/ddd", 4)) - "/data/"
       getParent(file("/data/aaa/bbb/cc/ddd", 5)) - null
       
      Parameters:
      file - 目录或文件
      level - 层级
      Returns:
      路径File, 如果不存在返回null
    • checkSlip

      public static File checkSlip(File parentFile, File file) throws IllegalArgumentException
      检查父完整路径是否为自路径的前半部分,如果不是说明不是子路径,可能存在slip注入

      见http://blog.nsfocus.net/zip-slip-2/

      Parameters:
      parentFile - 父文件或目录
      file - 子文件或目录
      Returns:
      子文件或目录
      Throws:
      IllegalArgumentException - 检查创建的子文件不在父目录中抛出此异常
    • getMediaType

      public static String getMediaType(String path)
      根据文件扩展名获得MediaType
      Parameters:
      path - 文件路径或文件名
      Returns:
      the string MediaType
    • getMediaType

      public static String getMediaType(Path file)
      获得文件的MediaType
      Parameters:
      file - 文件
      Returns:
      the string MediaType
    • removeSuffix

      public static String removeSuffix(String filename)
      从文件名中删除扩展名.

      此方法返回文件名最后一个点之前的文本部分,点之后必须没有目录分隔符.

       foo.txt    --> foo
       a\b\c.jpg  --> a\b\c
       a\b\c      --> a\b\c
       a.b\c      --> a.b\c
       
      Parameters:
      filename - 要查询的文件名null返回null
      Returns:
      文件名减去扩展名
    • isSuffix

      public static boolean isSuffix(String filename, String suffix)
      检查文件名的扩展名是否指定的扩展名
      Parameters:
      filename - 要查询的文件名,null返回false
      suffix - 要检查的扩展名,null检查是否没有扩展名
      Returns:
      如果文件名是扩展名之一,则为true
    • isSuffix

      public static boolean isSuffix(String filename, String[] suffixs)
      检查文件名的扩展名是否为指定的扩展名之一
      Parameters:
      filename - 要查询的文件名,null返回false
      suffixs - 要检查的扩展名,null检查是否没有扩展名
      Returns:
      如果文件名是扩展名之一,则为true
    • isSuffix

      public static boolean isSuffix(String filename, Collection<String> suffixs)
      检查文件名的扩展名是否为指定的扩展名之一
      Parameters:
      filename - 要查询的文件名,null返回false
      suffixs - 要检查的扩展名,null检查是否没有扩展名
      Returns:
      如果文件名是扩展名之一,则为true
    • tail

      public static void tail(File file, XConsumer<String> handler)
      文件内容跟随器,实现类似Linux下"tail -f"命令功能 此方法会阻塞当前线程
      Parameters:
      file - 文件
      handler - 行处理器
    • tail

      public static void tail(File file, Charset charset, XConsumer<String> handler)
      文件内容跟随器,实现类似Linux下"tail -f"命令功能 此方法会阻塞当前线程
      Parameters:
      file - 文件
      charset - 编码
      handler - 行处理器
    • tail

      public static void tail(File file, Charset charset)
      文件内容跟随器,实现类似Linux下"tail -f"命令功能 此方法会阻塞当前线程
      Parameters:
      file - 文件
      charset - 编码
    • createRandomAccessFile

      public static RandomAccessFile createRandomAccessFile(Path path, FileMode mode)
      Parameters:
      path - 文件Path
      mode - 模式,见FileMode
      Returns:
      RandomAccessFile
    • createRandomAccessFile

      public static RandomAccessFile createRandomAccessFile(File file, FileMode mode)
      Parameters:
      file - 文件
      mode - 模式,见FileMode
      Returns:
      RandomAccessFile
    • readers

      public static String readers(String resource)
      读取Classpath下的资源为字符串,使用UTF-8编码
      Parameters:
      resource - 资源路径,使用相对ClassPath的路径
      Returns:
      资源内容
    • readers

      public static String readers(String resource, Charset charset)
      读取Classpath下的资源为字符串
      Parameters:
      resource - 资源路径,使用相对ClassPath的路径
      charset - 编码
      Returns:
      资源内容
    • getReaders

      public static BufferedReader getReaders(String resurce, Charset charset)
      从ClassPath资源中获取BufferedReader
      Parameters:
      resurce - ClassPath资源
      charset - 编码
      Returns:
      InputStream
    • getStream

      public static InputStream getStream(String resurce)
      从ClassPath资源中获取InputStream
      Parameters:
      resurce - ClassPath资源
      Returns:
      InputStream
      Throws:
      InternalException - 资源不存在异常
    • getStreamSafe

      public static InputStream getStreamSafe(String resurce)
      从ClassPath资源中获取InputStream,当资源不存在时返回null
      Parameters:
      resurce - ClassPath资源
      Returns:
      InputStream
    • getUrl

      public static URL getUrl(String resource)
      获得资源的URL 路径用/分隔,例如:
       config/a/db.config
       spring/xml/test.xml
       
      Parameters:
      resource - 资源(相对Classpath的路径)
      Returns:
      资源URL
    • getUrl

      public static URL getUrl(String resource, Class<?> baseClass)
      获得资源相对路径对应的URL
      Parameters:
      resource - 资源相对路径,null和""都表示classpath根路径
      baseClass - 基准Class,获得的相对路径相对于此Class所在路径,如果为null则相对ClassPath
      Returns:
      URL
    • getUrls

      public static List<URL> getUrls(String resource)
      获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      Returns:
      资源列表
    • getUrls

      public static List<URL> getUrls(String resource, Predicate<URL> filter)
      获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      filter - 过滤器,用于过滤不需要的资源,null表示不过滤,保留所有元素
      Returns:
      资源列表
    • getResource

      public static Resource getResource(String path)
      获取Resource 资源对象 如果提供路径为绝对路径或路径以file:开头,返回FileResource,否则返回ClassPathResource
      Parameters:
      path - 路径,可以是绝对路径,也可以是相对路径(相对ClassPath)
      Returns:
      Resource 资源对象
    • getResource

      public static Resource getResource(URL url)
      获取UriResource 资源对象
      Parameters:
      url - URL
      Returns:
      Resource 资源对象
    • getResource

      public static Resource getResource(File file)
      获取FileResource 资源对象
      Parameters:
      file - File
      Returns:
      Resource 资源对象
    • isType

      public static boolean isType(String fileName, String... suffix)
      根据文件名检查文件类型,忽略大小写
      Parameters:
      fileName - 文件名,例如bus.png
      suffix - 被检查的扩展名数组,同一文件类型可能有多种扩展名,扩展名不带“.”
      Returns:
      是否是指定扩展名的类型
    • isEexist

      public static boolean isEexist(Path path, boolean isFollowLinks)
      判断文件或目录是否存在
      Parameters:
      path - 文件
      isFollowLinks - 是否跟踪软链(快捷方式)
      Returns:
      是否存在
    • isSub

      public static boolean isSub(File parent, File sub)
      判断给定的目录是否为给定文件或文件夹的子目录
      Parameters:
      parent - 父目录
      sub - 子目录
      Returns:
      子目录是否为父目录的子目录
    • isSub

      public static boolean isSub(Path parent, Path sub)
      判断给定的目录是否为给定文件或文件夹的子目录
      Parameters:
      parent - 父目录
      sub - 子目录
      Returns:
      子目录是否为父目录的子目录
    • toAbsNormal

      public static Path toAbsNormal(Path path)
      将Path路径转换为标准的绝对路径
      Parameters:
      path - 文件或目录Path
      Returns:
      转换后的Path
    • addContent

      public static void addContent(File dir, String content)
      向文件头部添加版权等内容
      Parameters:
      dir - 地址
      content - 内容