Package com.acanx.utils
Class FileUtil
java.lang.Object
com.acanx.utils.FileUtil
ACANX-Util / com.acanx.utils / FileUtil
文件由 ACANX 创建于 2019/1/5 . 15:47
FileUtil:
补充说明:
2019/1/5 15:47
- Since:
- 0.0.1
- Author:
- ACANX
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
deleteFile
(File file) 删除目标文件static boolean
fileExists
(File file) 文件是否存在getFileList
(File dir) 重载方法,支持传入多个目录getFileListSortedByLastModified
(String directory) 获取按最后修改时间排序的文件列表static String
getFileMD5
(File file) 计算文件的MD5static String
getFileSHA1
(File file) 计算文件的SHA1getFilteredFileList
(File dir, FileFilter fileFilter) 获取过滤后的文件列表static File[]
getSortedFilesByLastModified
(String directory) 获取按最后修改时间排序的文件列表static String
获取系统临时目录static void
移动文件static byte[]
readFileAsBytes
(String filePath) 根据文件路径读取byte[] 数组static String
readFileAsString
(String filePath) 读取文件内容,作为字符串返回static String
readFileToString
(File file) static String
readFileToString
(File file, String charsetName) Reads the contents of a file into a String.static String
readFileToString
(File file, Charset charset) Reads the contents of a file into a String.Reads the contents of a file line by line to a List of Strings.Reads the contents of a file line by line to a List of Strings.按行读取文件,将其保存到字符串集合中返回static void
write
(File file, CharSequence data) Writes a CharSequence to a file creating the file if it does not exist.static void
write
(File file, CharSequence data, Charset charset) Writes a CharSequence to a file creating the file if it does not exist.static void
writeLines
(File file, Collection<?> lines, Charset charset) 按行写入字符串集合
-
Method Details
-
getSysTempDir
获取系统临时目录- Returns:
- 系统临时目录路径字符串
-
fileExists
文件是否存在- Parameters:
file
- 文件- Returns:
- 判断结果
-
moveFile
移动文件- Parameters:
src
- 源文件dest
- 目标文件- Throws:
IOException
- 如果移动文件时发生错误
-
deleteFile
删除目标文件- Parameters:
file
- 目标文件- Returns:
- 文件删除结果
-
getFileList
重载方法,支持传入多个目录- Parameters:
dir
- 入口文件夹- Returns:
- 文件集合
-
getFilteredFileList
获取过滤后的文件列表- Parameters:
dir
- 入口文件fileFilter
- 过滤器实现类- Returns:
- 过滤后的文件集合
-
getFileMD5
计算文件的MD5- Parameters:
file
- 需要计算的文件- Returns:
- 计算的文件MD5值
-
getFileSHA1
计算文件的SHA1- Parameters:
file
- 需要计算SHA1的文件- Returns:
- 文件的SHA1值
-
readFileAsBytes
根据文件路径读取byte[] 数组- Parameters:
filePath
- 文件路径- Returns:
- 文本内容,byte[]类型
- Throws:
IOException
- IO异常
-
readLines
Reads the contents of a file line by line to a List of Strings. The file is always closed. Use Default CharSet UTF-8- Parameters:
file
- the file to read, must not benull
- Returns:
- the list of Strings representing each line in the file, never
null
- Throws:
NullPointerException
- if file isnull
.IOException
- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.UnsupportedCharsetException
- if the named charset is unavailable.- Since:
- 0.0.1.10
-
readLines
Reads the contents of a file line by line to a List of Strings. The file is always closed.- Parameters:
file
- the file to read, must not benull
charsetName
- the name of the requested charset,null
means platform default- Returns:
- the list of Strings representing each line in the file, never
null
- Throws:
NullPointerException
- if file isnull
.IOException
- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.UnsupportedCharsetException
- if the named charset is unavailable.- Since:
- 0.0.1.10
-
readLines
按行读取文件,将其保存到字符串集合中返回- Parameters:
file
- 需要读的文件charset
- 文件字符集- Returns:
- 字符串集合 一行一项
- Throws:
IOException
- IOException- Since:
- 0.0.1.10
-
readFileAsString
读取文件内容,作为字符串返回- Parameters:
filePath
- 文件路径- Returns:
- 文件内容
- Throws:
IOException
- 文件IO异常- Since:
- 0.0.1.10
-
readFileToString
- Throws:
IOException
-
readFileToString
Reads the contents of a file into a String. The file is always closed.- Parameters:
file
- the file to read, must not benull
charsetName
- the name of the requested charset,null
means platform default- Returns:
- the file contents, never
null
- Throws:
NullPointerException
- if file isnull
.IOException
- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.UnsupportedCharsetException
- if the named charset is unavailable.- Since:
- 0.0.1.10
-
readFileToString
Reads the contents of a file into a String. The file is always closed.- Parameters:
file
- the file to read, must not benull
charset
- the requested charset,null
means platform default- Returns:
- the file contents, never
null
- Throws:
NullPointerException
- if file isnull
.IOException
- if an I/O error occurs, including when the file does not exist, is a directory rather than a regular file, or for some other reason why the file cannot be opened for reading.- Since:
- 0.0.1.10
-
write
Writes a CharSequence to a file creating the file if it does not exist.- Parameters:
file
- the file to writedata
- the content to write to the file- Throws:
IOException
- in case of an I/O error- Since:
- 0.0.1.11
-
write
Writes a CharSequence to a file creating the file if it does not exist.- Parameters:
file
- the file to writedata
- the content to write to the filecharset
- the name of the requested charset,null
means platform default- Throws:
IOException
- in case of an I/O error- Since:
- 0.0.1.10
-
writeLines
@Alpha public static void writeLines(File file, Collection<?> lines, Charset charset) throws IOException 按行写入字符串集合- Parameters:
file
- 文件lines
- linescharset
- 字符集- Throws:
IOException
- IOException- Since:
- 0.0.1.10
-
getSortedFilesByLastModified
获取按最后修改时间排序的文件列表- Parameters:
directory
- 文件夹路径- Returns:
- 按最后修改时间排序的文件数组 最早修改的在前,最近修改/写入的排在最后
-
getFileListSortedByLastModified
获取按最后修改时间排序的文件列表- Parameters:
directory
- 文件夹路径- Returns:
- 按最后修改时间(正序)排序的文件列表
-