并发环境下,假设 test 目录不存在,如果线程A mkdirs "test/A" 目录,线程B mkdirs "test/B"目录,
其中一个线程可能会失败,进而导致以下代码抛出 FileNotFoundException 异常
file.getParentFile().mkdirs(); // 父目录正在被另一个线程创建中,返回 false
file.createNewFile(); // 抛出 IO 异常,因为该线程无法感知到父目录已被创建
1、src和dest都为目录,则将src目录及其目录下所有文件目录拷贝到dest下
2、src和dest都为文件,直接复制,名字为dest
3、src为文件,dest为目录,将src拷贝到dest目录下
1、src和dest都为目录,则将src下所有文件目录拷贝到dest下
2、src和dest都为文件,直接复制,名字为dest
3、src为文件,dest为目录,将src拷贝到dest目录下
1、src和dest都为目录,则将src下所有文件(不包括子目录)拷贝到dest下
2、src和dest都为文件,直接复制,名字为dest
3、src为文件,dest为目录,将src拷贝到dest目录下
给定路径已经是绝对路径
此方法并没有针对路径做标准化,建议先执行
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.
判断文件是否被改动
如果文件对象为 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.
获得一个文件读取器
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.
获得一个文件读取器
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.
读取文件内容
Parameters:
file
- 文件
charsetName
- 字符集
Returns:
Throws:
IORuntimeException
- IO异常
public static
String
readString(
String
path,
String
charsetName)
throws
IORuntimeException
Deprecated.
读取文件内容
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.
读取文件内容
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
Deprecated.
从文件中读取每一行数据
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.
从文件中读取每一行数据
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
Deprecated.
获得一个带缓存的写入对象
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
Deprecated.
获得一个带缓存的写入对象
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