Class FileKit


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

    • FileKit

      public FileKit()
  • Method Details

    • isWindows

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

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

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

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

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

      public static List<File> loopFiles(String path)
      递归遍历目录以及子目录中的所有文件 如果用户传入相对路径,则是相对classpath的路径 如:"test/aaa"表示"${classpath}/test/aaa"
      Parameters:
      path - 相对ClassPath的目录或者绝对路径目录
      Returns:
      文件列表
    • loopFiles

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

      public static void walkFiles(File file, Predicate<File> predicate)
      递归遍历目录并处理目录下的文件,可以处理目录或文件: 此方法与loopFiles(File, FileFilter)不同的是,处理目录判断,可减少无效目录的遍历。
      Parameters:
      file - 文件或目录,文件直接处理
      predicate - 文件处理器,只会处理文件
    • listFileNames

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

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

      public static File file(String path)
      创建File对象,自动识别相对或绝对路径,相对路径将自动从ClassPath下寻找
      Parameters:
      path - 相对ClassPath的目录或者绝对路径目录
      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对象 根据的路径构建文件,在Win下直接构建,在Linux下拆分路径单独构建 此方法会检查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 文件
    • 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
    • getTmpDir

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

      public static File getUserHomeDir()
      获取用户目录
      Returns:
      用户目录
    • 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
    • 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 - 目录或文件,null或者文件不存在返回0
      includeDirSize - 是否包括每层目录本身的大小
      Returns:
      总大小,bytes长度
    • getTotalLines

      public static int getTotalLines(File file)
      计算文件的总行数 读取文件采用系统默认编码,一般乱码不会造成行数错误。
      Parameters:
      file - 文件
      Returns:
      该文件总行数
    • getTotalLines

      public static int getTotalLines(File file, int bufferSize)
      计算文件的总行数 参考:https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
      Parameters:
      file - 文件
      bufferSize - 缓存大小,小于1则使用默认的1024
      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 - 相对ClassPath的目录或者绝对路径目录,使用POSIX风格
      Returns:
      文件,若路径为null,返回null
      Throws:
      InternalException - IO异常
    • touch

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

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

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

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

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

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

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

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

      public static void clean(File directory) throws InternalException
      清空文件夹 注意:清空文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹 某个文件删除失败会终止删除操作
      Parameters:
      directory - 文件夹
      Throws:
      InternalException - IO异常
    • 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:
      创建的目录
    • 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[Random].tmp
      Parameters:
      dir - 临时文件创建的所在目录
      Returns:
      临时文件
      Throws:
      InternalException - IO异常
    • createTempFile

      public static File createTempFile() throws InternalException
      在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Random].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[Random].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[Random].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[Random].tmp
      Parameters:
      dir - 临时文件创建的所在目录
      isReCreat - 是否重新创建文件(删掉原来的,创建新的)
      Returns:
      临时文件
      Throws:
      InternalException - IO异常
    • createTempFile

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

      public static File copy(Resource src, File target, boolean isOverride) throws InternalException
      拷贝资源到目标文件
      Parameters:
      src - 源文件
      target - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
      isOverride - 是否覆盖目标文件
      Returns:
      目标目录或文件
      Throws:
      InternalException - IO异常
    • copy

      public static File copy(InputStream src, File target, StandardCopyOption... options) throws InternalException
      通过JDK7+的 Files#copier(InputStream, Path, CopyOption...) 方法拷贝文件
      Parameters:
      src - 源文件流,使用后不关闭
      target - 目标文件
      options - StandardCopyOption
      Returns:
      目标文件
      Throws:
      InternalException - IO异常
    • copy

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

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

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

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

      public static File move(File src, File target, boolean isOverride) throws InternalException
      移动文件或目录到目标中,例如:
      • 如果src为文件,target为目录,则移动到目标目录下,存在同名文件则按照是否覆盖参数执行。
      • 如果src为文件,target为文件,则按照是否覆盖参数执行。
      • 如果src为文件,target为不存在的路径,则重命名源文件到目标指定的文件,如moveContent("/a/b", "/c/d"), d不存在,则b变成d。
      • 如果src为目录,target为文件,抛出IllegalArgumentException
      • 如果src为目录,target为目录,则将源目录及其内容移动到目标路径目录中,如move("/a/b", "/c/d"),结果为"/c/d/b"
      • 如果src为目录,target为不存在的路径,则创建目标路径为目录,将源目录及其内容移动到目标路径目录中,如move("/a/b", "/c/d"),结果为"/c/d/b"
      Parameters:
      src - 源文件或目录路径
      target - 目标路径,如果为目录,则移动到此目录下
      isOverride - 是否覆盖目标文件
      Returns:
      目标文件或目录
      Throws:
      InternalException - IO异常
      See Also:
    • 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 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:
      目标文件
      See Also:
    • getCanonicalPath

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

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

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

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

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

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

      public static boolean isDirectory(File file)
      判断是否为目录,如果file为null,则返回false
      Parameters:
      file - 文件
      Returns:
      如果为目录true
    • 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
    • equals

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

      public static boolean contentEquals(File file1, File file2) throws InternalException
      比较两个文件内容是否相同 首先比较长度,长度一致再比较内容 此方法来自Apache Commons io
      Parameters:
      file1 - 文件1
      file2 - 文件2
      Returns:
      两个文件内容一致返回true,否则false
      Throws:
      InternalException - IO异常
    • 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:
      是否相同
      Throws:
      InternalException - IO异常
    • 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. .. 和 . 转换为绝对路径,当..多于已有路径时,直接返回根路径
      5. 5. SMB路径保留,如\\127.0.0.0\a\b.zip
       "/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"
       "C:\\foo\\..\\bar" = "C:/bar"
       "C:\\..\\bar" = "C:/bar"
       "~/foo/../bar/" = "~/bar/"
       "~/../bar" = 普通用户运行是'bar的home目录',ROOT用户运行是'/bar'
       
      Parameters:
      path - 原路径
      Returns:
      修复后的路径
    • subPath

      public static String subPath(String rootDir, File file)
      获得相对子路径
       dirPath: d:/aaa/bbb    filePath: d:/aaa/bbb/ccc         =    ccc
       dirPath: d:/Aaa/bbb    filePath: d:/aaa/bbb/ccc.txt     =    ccc.txt
       
      Parameters:
      rootDir - 绝对父路径
      file - 文件
      Returns:
      相对子路径
    • subPath

      public static String subPath(String dirPath, String filePath)
      获得相对子路径,忽略大小写
       dirPath: d:/aaa/bbb    filePath: d:/aaa/bbb/ccc        =    ccc
       dirPath: d:/Aaa/bbb    filePath: d:/aaa/bbb/ccc.txt    =    ccc.txt
       dirPath: d:/Aaa/bbb    filePath: d:/aaa/bbb/           =    ""
       
      Parameters:
      dirPath - 父路径
      filePath - 文件路径
      Returns:
      相对子路径
    • pathEndsWith

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

      public static String getType(File file) throws InternalException
      根据文件流的头部信息获得文件类型
            1、无法识别类型默认按照扩展名识别
            2、xls、doc、msi头信息无法区分,按照扩展名区分
            3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
       
      Parameters:
      file - 文件 File
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - IO异常
      See Also:
    • getInputStream

      public static BufferedInputStream getInputStream(File file) throws InternalException
      获得输入流
      Parameters:
      file - 文件
      Returns:
      输入流
      Throws:
      InternalException - 文件未找到
      See Also:
    • 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 BomReader getBOMReader(File file)
      读取带BOM头的文件为Reader
      Parameters:
      file - 文件
      Returns:
      BufferedReader对象
    • getUtf8Reader

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      public static String readLine(RandomAccessFile file, Charset charset)
      单行处理文件内容
      Parameters:
      file - RandomAccessFile文件
      charset - 编码
      Returns:
      行内容
      Throws:
      InternalException - IO异常
    • readUtf8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      public static File writeBytes(byte[] data, String path) throws InternalException
      写数据到文件中 文件路径如果是相对路径,则相对ClassPath
      Parameters:
      data - 数据
      path - 相对ClassPath的目录或者绝对路径目录
      Returns:
      目标文件
      Throws:
      InternalException - IO异常
    • writeBytes

      public static File writeBytes(byte[] data, File dest) throws InternalException
      写数据到文件中
      Parameters:
      data - 数据
      dest - 目标文件
      Returns:
      目标文件
      Throws:
      InternalException - IO异常
    • 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 - IO异常
    • readableFileSize

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

      public static String readableFileSize(long size)
      Parameters:
      size - Long类型大小
      Returns:
      大小
      See Also:
    • convertCharset

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

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

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

      public static File getParent(File file, int level)
      获取指定层级的父路径
       getParent(file("d:/aaa/bbb/cc/ddd", 0)) - "d:/aaa/bbb/cc/ddd"
       getParent(file("d:/aaa/bbb/cc/ddd", 2)) - "d:/aaa/bbb"
       getParent(file("d:/aaa/bbb/cc/ddd", 4)) - "d:/"
       getParent(file("d:/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 - 检查创建的子文件不在父目录中抛出此异常
    • getMimeType

      public static String getMimeType(String filePath, String defaultValue)
      根据文件扩展名获得MimeType
      Parameters:
      filePath - 文件路径或文件名
      defaultValue - 当获取MimeType为null时的默认值
      Returns:
      MimeType
    • getMimeType

      public static String getMimeType(String filePath)
      根据文件扩展名获得MimeType
      Parameters:
      filePath - 文件路径或文件名
      Returns:
      MimeType
    • isSymlink

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

      public static boolean isSub(File parent, File sub)
      判断给定的目录是否为给定文件或文件夹的子目录
      Parameters:
      parent - 父目录,非空
      sub - 子目录,非空
      Returns:
      子目录是否为父目录的子目录
    • 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
    • tail

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

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

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

      public static FileSystem of(String path)
      创建 FileSystem
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      Returns:
      FileSystem
    • createZip

      public static FileSystem createZip(String path)
      创建 Zip的FileSystem,默认UTF-8编码
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      Returns:
      FileSystem
    • createZip

      public static FileSystem createZip(String path, Charset charset)
      创建 Zip的FileSystem
      Parameters:
      path - 文件路径,可以是目录或Zip文件等
      charset - 编码
      Returns:
      FileSystem
    • getRoot

      public static Path getRoot(FileSystem fileSystem)
      获取目录的根路径,或Zip文件中的根路径
      Parameters:
      fileSystem - FileSystem
      Returns:
      Path