添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 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 isAbsolutePath ( String path)
给定路径已经是绝对路径
此方法并没有针对路径做标准化,建议先执行 normalize(String) 方法标准化路径后判断
绝对路径判断条件是: 以/开头的路径 满足类似于 c:/xxxxx,其中祖母随意,不区分大小写 满足类似于 d:\xxxxx,其中祖母随意,不区分大小写 并发环境下,假设 test 目录不存在,如果线程A mkdirs "test/A" 目录,线程B mkdirs "test/B"目录, 其中一个线程可能会失败,进而导致以下代码抛出 FileNotFoundException 异常 file.getParentFile().mkdirs(); // 父目录正在被另一个线程创建中,返回 false file.createNewFile(); // 抛出 IO 异常,因为该线程无法感知到父目录已被创建 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/ =》 ""

Methods inherited from class cn.hutool.core.io.file. PathUtil

copy , copyContent , copyFile , copyFile , copyFile , copyFile , createTempFile , del , delFile , equals , exists , getAttributes , getFileVisitOption , getInputStream , getLastPathEle , getLinkOptions , getMimeType , getName , getOutputStream , getPathEle , getReader , getUtf8Reader , isDirectory , isDirectory , isDirEmpty , isExistsAndNotDirectory , isFile , isSub , isSymlink , loopFiles , loopFiles , loopFiles , mkdir , mkParentDirs , move , moveContent , readBytes , rename , subPath , toAbsNormal , walkFiles , walkFiles , walkFiles
public static List<File> loopFiles(String path,
                                   FileFilter fileFilter)
递归遍历目录以及子目录中的所有文件
如果提供file为文件,直接返回过滤结果
Parameters:
path - 当前遍历文件或目录的路径
fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录
Returns:
Since:
3.2.0
public static void walkFiles(File file,
                             Consumer<File> consumer)
递归遍历目录并处理目录下的文件,可以处理目录或文件:
  • 非目录则直接调用 Consumer 处理
  • 目录则递归调用此方法处理
  • Parameters:
    file - 文件或目录,文件直接处理
    consumer - 文件处理器,只会处理文件
    Since:
    5.5.2
    file - 当前遍历文件或目录
    maxDepth - 遍历最大深度,-1表示遍历到没有目录为止
    fileFilter - 文件过滤规则对象,选择要保留的文件,只对文件有效,不过滤目录,null表示接收全部文件
    Returns:
    Since:
    4.6.3
    public static List<File> loopFiles(String path)
    递归遍历目录以及子目录中的所有文件
    如果用户传入相对路径,则是相对classpath的路径
    如:"test/aaa"表示"${classpath}/test/aaa"
    Parameters:
    path - 相对ClassPath的目录或者绝对路径目录
    Returns:
    Since:
    3.2.0

    listFileNames

    public static List<String> listFileNames(String path)
                                      throws IORuntimeException
    获得指定目录下所有文件
    不会扫描子目录
    如果用户传入相对路径,则是相对classpath的路径
    如:"test/aaa"表示"${classpath}/test/aaa"
    Parameters:
    path - 相对ClassPath的目录或者绝对路径目录
    Returns:
    文件路径列表(如果是jar中的文件,则给定类似.jar!/xxx/xxx的路径)
    Throws:
    IORuntimeException - IO异常
    创建File对象
    根据的路径构建文件,在Win下直接构建,在Linux下拆分路径单独构建 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
    Parameters:
    parent - 父文件对象
    path - 文件路径
    Returns:
    public static int getTotalLines(File file)
    计算文件的总行数
    参考:https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
    Parameters:
    file - 文件
    Returns:
    该文件总行数
    Since:
    5.7.22
    int bufferSize)
    计算文件的总行数
    参考:https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
    Parameters:
    file - 文件
    bufferSize - 缓存大小,小于1则使用默认的1024
    Returns:
    该文件总行数
    Since:
    5.8.28
    public static File touch(String path)
                      throws IORuntimeException
    创建文件及其父目录,如果这个文件存在,直接返回这个文件
    此方法不对File对象类型做判断,如果File不存在,无法判断其类型
    Parameters:
    path - 相对ClassPath的目录或者绝对路径目录,使用POSIX风格
    Returns:
    文件,若路径为null,返回null
    Throws:
    IORuntimeException - IO异常
    public static File touch(File file)
                      throws IORuntimeException
    创建文件及其父目录,如果这个文件存在,直接返回这个文件
    此方法不对File对象类型做判断,如果File不存在,无法判断其类型
    Parameters:
    file - 文件对象
    Returns:
    文件,若路径为null,返回null
    Throws:
    IORuntimeException - IO异常
    throws IORuntimeException
    删除文件或者文件夹
    路径如果为相对路径,会转换为ClassPath路径! 注意:删除文件夹时不会判断文件夹是否为空,如果不空则递归删除子文件或文件夹
    某个文件删除失败会终止删除操作
    Parameters:
    fullFileOrDirPath - 文件或者目录的路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    从5.7.6开始,删除文件使用 Files.delete(Path) 代替 File.delete()
    因为前者遇到文件被占用等原因时,抛出异常,而非返回false,异常会指明具体的失败原因。
    Parameters:
    file - 文件对象
    Returns:
    Throws:
    IORuntimeException - IO异常
    See Also:
    Files.delete(Path)
    public static File mkdir(String dirPath)
    创建文件夹,如果存在直接返回此文件夹
    此方法不对File对象类型做判断,如果File不存在,无法判断其类型
    Parameters:
    dirPath - 文件夹路径,使用POSIX格式,无论哪个平台
    Returns:
    创建的目录
    public static File mkdir(File dir)
    创建文件夹,会递归自动创建其不存在的父文件夹,如果存在直接返回此文件夹
    此方法不对File对象类型做判断,如果File不存在,无法判断其类型
    Parameters:
    dir - 目录
    Returns:
    创建的目录
    并发环境下,假设 test 目录不存在,如果线程A mkdirs "test/A" 目录,线程B mkdirs "test/B"目录, 其中一个线程可能会失败,进而导致以下代码抛出 FileNotFoundException 异常 file.getParentFile().mkdirs(); // 父目录正在被另一个线程创建中,返回 false file.createNewFile(); // 抛出 IO 异常,因为该线程无法感知到父目录已被创建
    Parameters:
    dir - 待创建的目录
    tryCount - 最大尝试次数
    sleepMillis - 线程等待的毫秒数
    Returns:
    true表示创建成功,false表示创建失败
    Since:
    5.7.21
    public static File createTempFile()
                               throws IORuntimeException
    在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Random].tmp。 默认临时文件目录由系统属性 java.io.tmpdir 指定。 在 UNIX 系统上,此属性的默认值通常是 "tmp" "vartmp" ; 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 。 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响。
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.7.22
    public static File createTempFile(String suffix,
                                      boolean isReCreat)
                               throws IORuntimeException
    在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Random].suffix。 默认临时文件目录由系统属性 java.io.tmpdir 指定。 在 UNIX 系统上,此属性的默认值通常是 "tmp" "vartmp" ; 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 。 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响。
    Parameters:
    suffix - 后缀,如果null则使用默认.tmp
    isReCreat - 是否重新创建文件(删掉原来的,创建新的)
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.7.22
    boolean isReCreat) throws IORuntimeException
    在默认临时文件目录下创建临时文件,创建后的文件名为 prefix[Random].suffix。 默认临时文件目录由系统属性 java.io.tmpdir 指定。 在 UNIX 系统上,此属性的默认值通常是 "tmp" "vartmp" ; 在 Microsoft Windows 系统上,它通常是 "C:\\WINNT\\TEMP" 。 调用 Java 虚拟机时,可以为该系统属性赋予不同的值,但不保证对该属性的编程更改对该方法使用的临时目录有任何影响。
    Parameters:
    prefix - 前缀,至少3个字符
    suffix - 后缀,如果null则使用默认.tmp
    isReCreat - 是否重新创建文件(删掉原来的,创建新的)
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.7.22
    public static File createTempFile(File dir,
                                      boolean isReCreat)
                               throws IORuntimeException
    创建临时文件
    创建后的文件名为 prefix[Random].tmp
    Parameters:
    dir - 临时文件创建的所在目录
    isReCreat - 是否重新创建文件(删掉原来的,创建新的)
    Returns:
    Throws:
    IORuntimeException - IO异常
    StandardCopyOption ... options) throws IORuntimeException
    通过JDK7+的 Files#copy(Path, Path, CopyOption...) 方法拷贝文件
    Parameters:
    src - 源文件路径
    dest - 目标文件或目录路径,如果为目录使用与源文件相同的文件名
    options - StandardCopyOption
    Returns:
    Throws:
    IORuntimeException - IO异常
    StandardCopyOption ... options) throws IORuntimeException
    通过JDK7+的 Files#copy(InputStream, Path, CopyOption...) 方法拷贝文件
    Parameters:
    src - 源文件
    dest - 目标文件或目录,如果为目录使用与源文件相同的文件名
    options - StandardCopyOption
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.8.27
    public static File copyFile(InputStream src,
                                File dest,
                                StandardCopyOption... options)
                         throws IORuntimeException
    通过JDK7+的 Files#copy(InputStream, Path, CopyOption...) 方法拷贝文件
    Parameters:
    src - 源文件流,使用后不关闭
    dest - 目标文件,不存在自动创建
    options - StandardCopyOption
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.8.27
    StandardCopyOption ... options) throws IORuntimeException
    通过JDK7+的 Files#copy(Path, Path, CopyOption...) 方法拷贝文件
    Parameters:
    src - 源文件
    dest - 目标文件或目录,如果为目录使用与源文件相同的文件名
    options - StandardCopyOption
    Returns:
    Throws:
    IORuntimeException - IO异常
    1、src和dest都为目录,则将src目录及其目录下所有文件目录拷贝到dest下 2、src和dest都为文件,直接复制,名字为dest 3、src为文件,dest为目录,将src拷贝到dest目录下
    Parameters:
    src - 源文件
    dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
    isOverride - 是否覆盖目标文件
    Returns:
    目标目录或文件
    Throws:
    IORuntimeException - IO异常
    1、src和dest都为目录,则将src下所有文件目录拷贝到dest下 2、src和dest都为文件,直接复制,名字为dest 3、src为文件,dest为目录,将src拷贝到dest目录下
    Parameters:
    src - 源文件
    dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
    isOverride - 是否覆盖目标文件
    Returns:
    目标目录或文件
    Throws:
    IORuntimeException - IO异常
    1、src和dest都为目录,则将src下所有文件(不包括子目录)拷贝到dest下 2、src和dest都为文件,直接复制,名字为dest 3、src为文件,dest为目录,将src拷贝到dest目录下
    Parameters:
    src - 源文件
    dest - 目标文件或目录,目标不存在会自动创建(目录、文件都创建)
    isOverride - 是否覆盖目标文件
    Returns:
    目标目录或文件
    Throws:
    IORuntimeException - IO异常
    Since:
    4.1.5
    newName - 新的文件名,可选是否包括扩展名
    isRetainExt - 是否保留原文件的扩展名,如果保留,则newName不需要加扩展名
    isOverride - 是否覆盖目标文件
    Returns:
    Since:
    3.0.9
    See Also:
    PathUtil.rename(Path, String, boolean)

    getAbsolutePath

    public static String getAbsolutePath(String path)
    获取绝对路径,相对于ClassPath的目录
    如果给定就是绝对路径,则返回原路径,原路径把所有\替换为/
    兼容Spring风格的路径表示,例如:classpath:config/example.setting也会被识别后转换
    Parameters:
    path - 相对路径
    Returns:
    public static boolean isAbsolutePath(String path)
    给定路径已经是绝对路径
    此方法并没有针对路径做标准化,建议先执行 normalize(String) 方法标准化路径后判断
    绝对路径判断条件是:
  • 以/开头的路径
  • 满足类似于 c:/xxxxx,其中祖母随意,不区分大小写
  • 满足类似于 d:\xxxxx,其中祖母随意,不区分大小写
  • Parameters:
    path - 需要检查的Path
    Returns:
    是否已经是绝对路径

    contentEqualsIgnoreEOL

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

    lastIndexOfSeparator

    public static int lastIndexOfSeparator(String filePath)
    获得最后一个文件路径分隔符的位置
    Parameters:
    filePath - 文件路径
    Returns:
    最后一个文件路径分隔符的位置
    public static boolean isModifed( File file, long lastModifyTime)
    Deprecated. 拼写错误,请使用 isModified(File, long)
    判断文件是否被改动
    如果文件对象为 null 或者文件不存在,被视为改动
    Parameters:
    file - 文件对象
    lastModifyTime - 上次的改动时间
    Returns:
    是否被改动
    "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:
    修复后的路径
    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:
    相对子路径
    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:
    相对子路径
    1、无法识别类型默认按照扩展名识别 2、xls、doc、msi头信息无法区分,按照扩展名区分 3、zip可能为docx、xlsx、pptx、jar、war头信息无法区分,按照扩展名区分
    Parameters:
    file - 文件 File
    Returns:
    类型,文件的扩展名,未找到为 null
    Throws:
    IORuntimeException - IO异常
    See Also:
    FileTypeUtil.getType(File)

    getInputStream

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

    getInputStream

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

    getBOMInputStream

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

    getUtf8Reader

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

    getUtf8Reader

    public static BufferedReader getUtf8Reader(String path)
                                        throws IORuntimeException
    获得一个文件读取器
    Parameters:
    path - 文件路径
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedReader getReader( File file, String charsetName) throws IORuntimeException
    Deprecated. 请使用 getReader(File, Charset)
    获得一个文件读取器
    Parameters:
    file - 文件
    charsetName - 字符集
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedReader getReader(File file,
                                           Charset charset)
                                    throws IORuntimeException
    获得一个文件读取器
    Parameters:
    file - 文件
    charset - 字符集
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedReader getReader( String path, String charsetName) throws IORuntimeException
    Deprecated. 请使用 getReader(String, Charset)
    获得一个文件读取器
    Parameters:
    path - 绝对路径
    charsetName - 字符集
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedReader getReader(String path,
                                           Charset charset)
                                    throws IORuntimeException
    获得一个文件读取器
    Parameters:
    path - 绝对路径
    charset - 字符集
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常

    readUtf8String

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

    readUtf8String

    public static String readUtf8String(String path)
                                 throws IORuntimeException
    读取文件内容
    Parameters:
    path - 文件路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static String readString( File file, String charsetName) throws IORuntimeException
    Deprecated. 请使用 readString(File, Charset)
    读取文件内容
    Parameters:
    file - 文件
    charsetName - 字符集
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static String readString( String path, String charsetName) throws IORuntimeException
    Deprecated. 请使用 readString(String, Charset)
    读取文件内容
    Parameters:
    path - 文件路径
    charsetName - 字符集
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static String readString(String path,
                                    Charset charset)
                             throws IORuntimeException
    读取文件内容
    Parameters:
    path - 文件路径
    charset - 字符集
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static String readString( URL url, String charsetName) throws IORuntimeException
    Deprecated. 请使用 readString(URL, Charset)
    读取文件内容
    Parameters:
    url - 文件URL
    charsetName - 字符集
    Returns:
    Throws:
    IORuntimeException - IO异常

    readUtf8Lines

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

    readLines

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

    readLines

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

    readUtf8Lines

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

    readLines

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

    readLines

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

    readUtf8Lines

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

    readLines

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

    readUtf8Lines

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

    readUtf8Lines

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

    readUtf8Lines

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

    readUtf8Lines

    public static List<String> readUtf8Lines(File file,
                                             Predicate<String> filter)
                                      throws IORuntimeException
    从文件中读取每一行数据
    Parameters:
    file - 文件
    filter - 过滤器
    Returns:
    文件中的每行内容的集合List
    Throws:
    IORuntimeException - IO异常
    Since:
    3.1.1
    public static List<String> readLines(File file,
                                         String charset)
                                  throws IORuntimeException
    从文件中读取每一行数据
    Parameters:
    file - 文件
    charset - 字符集
    Returns:
    文件中的每行内容的集合List
    Throws:
    IORuntimeException - IO异常
    public static List<String> readLines(File file,
                                         Charset charset)
                                  throws IORuntimeException
    从文件中读取每一行数据
    Parameters:
    file - 文件
    charset - 字符集
    Returns:
    文件中的每行内容的集合List
    Throws:
    IORuntimeException - IO异常
    public static List<String> readLines(File file,
                                         Charset charset,
                                         Predicate<String> filter)
                                  throws IORuntimeException
    从文件中读取每一行数据
    Parameters:
    file - 文件
    charset - 字符集
    filter - 过滤器
    Returns:
    文件中的每行内容的集合List
    Throws:
    IORuntimeException - IO异常
    public static void readUtf8Lines(File file,
                                     LineHandler lineHandler)
                              throws IORuntimeException
    按行处理文件内容,编码为UTF-8
    Parameters:
    file - 文件
    lineHandler - LineHandler 行处理器
    Throws:
    IORuntimeException - IO异常
    public static void readLines(RandomAccessFile file,
                                 Charset charset,
                                 LineHandler lineHandler)
    按行处理文件内容
    Parameters:
    file - RandomAccessFile 文件
    charset - 编码
    lineHandler - LineHandler 行处理器
    Throws:
    IORuntimeException - IO异常
    Since:
    4.5.2
    public static void readLine(RandomAccessFile file,
                                Charset charset,
                                LineHandler lineHandler)
    单行处理文件内容
    Parameters:
    file - RandomAccessFile 文件
    charset - 编码
    lineHandler - LineHandler 行处理器
    Throws:
    IORuntimeException - IO异常
    Since:
    4.5.2

    loadUtf8

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

    loadUtf8

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

    getOutputStream

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

    getOutputStream

    public static BufferedOutputStream getOutputStream(String path)
                                                throws IORuntimeException
    获得一个输出流对象
    Parameters:
    path - 输出到的文件路径,绝对路径
    Returns:
    输出流对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedWriter getWriter( String path, String charsetName, boolean isAppend) throws IORuntimeException
    获得一个带缓存的写入对象
    Parameters:
    path - 输出路径,绝对路径
    charsetName - 字符集
    isAppend - 是否追加
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedWriter getWriter(String path,
                                           Charset charset,
                                           boolean isAppend)
                                    throws IORuntimeException
    获得一个带缓存的写入对象
    Parameters:
    path - 输出路径,绝对路径
    charset - 字符集
    isAppend - 是否追加
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static BufferedWriter getWriter( File file, String charsetName, boolean isAppend) throws IORuntimeException
    获得一个带缓存的写入对象
    Parameters:
    file - 输出文件
    charsetName - 字符集
    isAppend - 是否追加
    Returns:
    BufferedReader对象
    Throws:
    IORuntimeException - IO异常
    public static PrintWriter getPrintWriter(String path,
                                             String charset,
                                             boolean isAppend)
                                      throws IORuntimeException
    获得一个打印写入对象,可以有print
    Parameters:
    path - 输出路径,绝对路径
    charset - 字符集
    isAppend - 是否追加
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static PrintWriter getPrintWriter(String path,
                                             Charset charset,
                                             boolean isAppend)
                                      throws IORuntimeException
    获得一个打印写入对象,可以有print
    Parameters:
    path - 输出路径,绝对路径
    charset - 字符集
    isAppend - 是否追加
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    4.1.1
    public static PrintWriter getPrintWriter(File file,
                                             Charset charset,
                                             boolean isAppend)
                                      throws IORuntimeException
    获得一个打印写入对象,可以有print
    Parameters:
    file - 文件
    charset - 字符集
    isAppend - 是否追加
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    5.4.3
    public static File writeUtf8String(String content,
                                       String path)
                                throws IORuntimeException
    将String写入文件,覆盖模式,字符集为UTF-8
    Parameters:
    content - 写入的内容
    path - 文件路径
    Returns:
    写入的文件
    Throws:
    IORuntimeException - IO异常
    public static File writeUtf8String(String content,
                                       File file)
                                throws IORuntimeException
    将String写入文件,覆盖模式,字符集为UTF-8
    Parameters:
    content - 写入的内容
    file - 文件
    Returns:
    写入的文件
    Throws:
    IORuntimeException - IO异常
    public static File appendUtf8String(String content,
                                        String path)
                                 throws IORuntimeException
    将String写入文件,UTF-8编码追加模式
    Parameters:
    content - 写入的内容
    path - 文件路径
    Returns:
    写入的文件
    Throws:
    IORuntimeException - IO异常
    Since:
    3.1.2
    public static File appendUtf8String(String content,
                                        File file)
                                 throws IORuntimeException
    将String写入文件,UTF-8编码追加模式
    Parameters:
    content - 写入的内容
    file - 文件
    Returns:
    写入的文件
    Throws:
    IORuntimeException - IO异常
    Since:
    3.1.2
    public static <T> File writeUtf8Lines(Collection<T> list,
                                          String path)
                                   throws IORuntimeException
    将列表写入文件,覆盖模式,编码为UTF-8
    Type Parameters:
    T - 集合元素类型
    Parameters:
    list - 列表
    path - 绝对路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    3.2.0
    public static <T> File writeUtf8Lines(Collection<T> list,
                                          File file)
                                   throws IORuntimeException
    将列表写入文件,覆盖模式,编码为UTF-8
    Type Parameters:
    T - 集合元素类型
    Parameters:
    list - 列表
    file - 绝对路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    3.2.0
    public static <T> File appendUtf8Lines(Collection<T> list,
                                           File file)
                                    throws IORuntimeException
    将列表写入文件,追加模式
    Type Parameters:
    T - 集合元素类型
    Parameters:
    list - 列表
    file - 文件
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    3.1.2

    appendUtf8Lines

    public static <T> File appendUtf8Lines(Collection<T> list,
                                           String path)
                                    throws IORuntimeException
    将列表写入文件,追加模式
    Type Parameters:
    T - 集合元素类型
    Parameters:
    list - 列表
    path - 文件路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    Since:
    3.1.2
    public static File writeFromStream(InputStream in,
                                       File dest)
                                throws IORuntimeException
    将流的内容写入文件
    此方法会自动关闭输入流
    Parameters:
    dest - 目标文件
    in - 输入流
    Returns:
    Throws:
    IORuntimeException - IO异常

    writeFromStream

    public static File writeFromStream(InputStream in,
                                       String fullFilePath)
                                throws IORuntimeException
    将流的内容写入文件
    此方法会自动关闭输入流
    Parameters:
    in - 输入流
    fullFilePath - 文件绝对路径
    Returns:
    Throws:
    IORuntimeException - IO异常
    public static long writeToStream(String fullFilePath,
                                     OutputStream out)
                              throws IORuntimeException
    将路径对应文件写入流中,此方法不会关闭输出流
    Parameters:
    fullFilePath - 文件绝对路径
    out - 输出流
    Returns:
    写出的流byte数
    Throws:
    IORuntimeException - IO异常
    public static String readableFileSize(long size)
    Parameters:
    size - Long类型大小
    Returns:
    See Also:
    DataSizeUtil.format(long)
    public static File convertCharset(File file,
                                      Charset srcCharset,
                                      Charset destCharset)
    转换文件编码
    此方法用于转换文件编码,读取的文件实际编码必须与指定的srcCharset编码一致,否则导致乱码
    Parameters:
    file - 文件
    srcCharset - 原文件的编码,必须与文件内容的编码保持一致
    destCharset - 转码后的编码
    Returns:
    被转换编码的文件
    Since:
    3.1.0
    See Also:
    CharsetUtil.convert(File, Charset, Charset)

    convertLineSeparator

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

    cleanInvalid

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

    containsInvalid

    public static boolean containsInvalid(String fileName)
    文件名中是否包含在Windows下不支持的非法字符,包括: \ / : * ? " < > |
    Parameters:
    fileName - 文件名(必须不包括路径,否则路径符将被替换)
    Returns:
    是否包含非法字符
    Since:
    3.3.1
    See Also:
    FileNameUtil.containsInvalid(String)
    public static Checksum checksum(File file,
                                    Checksum checksum)
                             throws IORuntimeException
    计算文件校验码
    Parameters:
    file - 文件,不能为目录
    checksum - Checksum
    Returns:
    Checksum
    Throws:
    IORuntimeException - IO异常
    Since:
    4.0.6
    public static File getWebRoot()
    获取Web项目下的web root路径
    原理是首先获取ClassPath路径,由于在web项目中ClassPath位于 WEB-INF/classes/下,故向上获取两级目录即可。
    Returns:
    web root路径
    Since:
    4.0.13
    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
    Since:
    4.1.2
    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
    Since:
    4.1.2
    public static File checkSlip(File parentFile,
                                 File file)
                          throws IllegalArgumentException
    检查父完整路径是否为自路径的前半部分,如果不是说明不是子路径,可能存在slip注入。 见http://blog.nsfocus.net/zip-slip-2/

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

    createRandomAccessFile

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

    createRandomAccessFile

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