public class FileUtil extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
FileUtil.ReaderHandler<T>
Reader处理接口
|
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
CLASS_EXT
Class文件扩展名
|
static String |
JAR_FILE_EXT
Jar文件扩展名
|
static String |
JAR_PATH_EXT
在Jar中的路径jar的扩展名形式
|
static String |
PATH_FILE_PRE
当Path为文件形式时, path会加入一个表示文件的前缀
|
| 构造器和说明 |
|---|
FileUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> void |
appendLines(Collection<T> list,
String path,
String charset)
将列表写入文件,追加模式
|
static void |
appendString(String content,
String path,
String charset)
将String写入文件,追加模式
|
static void |
close(Closeable closeable)
关闭
|
static void |
copy(File src,
File dest,
boolean isOverride)
复制文件
如果目标文件为目录,则将源文件以相同文件名拷贝到目标目录 |
static File |
createTempFile(String prefix,
String suffix,
File dir,
boolean isReCreat)
创建临时文件
创建后的文件名为 prefix[Randon].suffix From com.jodd.io.FileUtil |
static boolean |
del(File file)
删除文件或者文件夹
|
static boolean |
del(String fullFileOrDirPath)
删除文件或者文件夹
|
static boolean |
equals(File file1,
File file2)
检查两个文件是否是同一个文件
|
static String |
getAbsolutePath(String path)
获取绝对路径,相对于classes的根目录
如果给定就是绝对路径,则返回原路径,原路径把所有\替换为/ |
static String |
getAbsolutePath(String path,
Class<?> baseClass)
获取绝对路径
此方法不会判定给定路径是否有效(文件或目录存在) |
static BufferedWriter |
getBufferedWriter(String path,
String charset,
boolean isAppend)
获得一个带缓存的写入对象
|
static String |
getExtension(String fileName)
获得文件的扩展名
|
static OutputStream |
getOutputStream(String path)
获得一个输出流对象
|
static PrintWriter |
getPrintWriter(String path,
String charset,
boolean isAppend)
获得一个打印写入对象,可以有print
|
static BufferedReader |
getReader(String path,
String charset)
获得一个文件读取器
|
static int |
indexOfLastSeparator(String filePath)
获得最后一个文件路径分隔符的位置
|
static boolean |
isExist(String path)
文件是否存在
|
static boolean |
isModifed(File file,
long lastModifyTime)
判断文件是否被改动
如果文件对象为 null 或者文件不存在,被视为改动 |
static List<String> |
listFileNames(String path)
获得指定目录下所有文件
不会扫描子目录 |
static <T> T |
load(FileUtil.ReaderHandler<T> readerHandler,
String path,
String charset)
按照给定的readerHandler读取文件中的数据
|
static File[] |
ls(String path)
列出目录文件
给定的绝对路径不能是压缩包中的路径 |
static File |
mkdir(String dirPath)
创建文件夹,如果存在直接返回此文件夹
|
static void |
move(File src,
File dest,
boolean isOverride)
移动文件或者目录
|
static byte[] |
readBytes(File file)
读取文件所有数据
文件的长度不能超过Integer.MAX_VALUE |
static List<String> |
readLines(String path,
String charset)
从文件中读取每一行数据
|
static <T extends Collection<String>> |
readLines(String path,
String charset,
T collection)
从文件中读取每一行数据
|
static List<String> |
readLines(URL url,
String charset)
从文件中读取每一行数据
|
static <T extends Collection<String>> |
readLines(URL url,
String charset,
T collection)
从文件中读取每一行数据
|
static String |
readString(String path,
String charset)
读取文件内容
|
static String |
readString(URL url,
String charset)
读取文件内容
|
static File |
touch(String fullFilePath)
创建文件,如果这个文件存在,直接返回这个文件
|
static void |
writeBytes(byte[] data,
String path)
写数据到文件中
|
static void |
writeBytes(File dest,
byte[] data)
写数据到文件中
|
static void |
writeBytes(File dest,
byte[] data,
int off,
int len,
boolean append)
写入数据到文件
|
static <T> void |
writeLines(Collection<T> list,
String path,
String charset)
将列表写入文件,覆盖模式
|
static <T> void |
writeLines(Collection<T> list,
String path,
String charset,
boolean isAppend)
将列表写入文件
|
static void |
writeStream(File dest,
InputStream in)
将流的内容写入文件
|
static void |
writeStream(String fullFilePath,
InputStream in)
将流的内容写入文件
|
static void |
writeString(String content,
String path,
String charset)
将String写入文件,覆盖模式
|
public static File[] ls(String path)
path - 目录绝对路径或者相对路径public static List<String> listFileNames(String path)
path - 相对ClassPath的目录或者绝对路径目录IOExceptionpublic static File touch(String fullFilePath) throws IOException
fullFilePath - 文件的全路径,使用POSIX风格IOExceptionpublic static boolean del(String fullFileOrDirPath) throws IOException
fullFileOrDirPath - 文件或者目录的路径IOExceptionpublic static boolean del(File file) throws IOException
file - 文件对象IOExceptionpublic static File mkdir(String dirPath)
dirPath - 文件夹路径,使用POSIX格式,无论哪个平台public static File createTempFile(String prefix, String suffix, File dir, boolean isReCreat) throws IOException
prefix - 前缀,至少3个字符suffix - 后缀,如果null则使用默认.tmpdir - 临时文件创建的所在目录isReCreat - 是否重新创建文件(删掉原来的,创建新的)IOExceptionpublic static void copy(File src, File dest, boolean isOverride) throws IOException
src - 源文件dest - 目标文件或目录isOverride - 是否覆盖目标文件IOExceptionpublic static void move(File src, File dest, boolean isOverride) throws IOException
src - 源文件或者目录dest - 目标文件或者目录isOverride - 是否覆盖目标IOExceptionpublic static String getAbsolutePath(String path, Class<?> baseClass)
path - 相对路径baseClass - 相对路径所相对的类public static String getAbsolutePath(String path)
path - 相对路径public static boolean isExist(String path)
path - 文件路径public static void close(Closeable closeable)
closeable - 被关闭的对象public static boolean equals(File file1, File file2)
file1 - 文件1file2 - 文件2public static BufferedWriter getBufferedWriter(String path, String charset, boolean isAppend) throws IOException
path - 输出路径,绝对路径charset - 字符集isAppend - 是否追加IOExceptionpublic static PrintWriter getPrintWriter(String path, String charset, boolean isAppend) throws IOException
path - 输出路径,绝对路径charset - 字符集isAppend - 是否追加IOExceptionpublic static OutputStream getOutputStream(String path) throws IOException
path - 输出到的文件路径,绝对路径IOExceptionpublic static BufferedReader getReader(String path, String charset) throws IOException
path - 绝对路径charset - 字符集IOExceptionpublic static <T extends Collection<String>> T readLines(String path, String charset, T collection) throws IOException
path - 文件路径charset - 字符集collection - 集合IOExceptionpublic static <T extends Collection<String>> T readLines(URL url, String charset, T collection) throws IOException
url - 文件的URLcharset - 字符集collection - 集合IOExceptionpublic static List<String> readLines(URL url, String charset) throws IOException
url - 文件的URLcharset - 字符集IOExceptionpublic static List<String> readLines(String path, String charset) throws IOException
path - 文件路径charset - 字符集IOExceptionpublic static <T> T load(FileUtil.ReaderHandler<T> readerHandler, String path, String charset) throws IOException
readerHandler - Reader处理类path - 文件的绝对路径charset - 字符集IOExceptionpublic static int indexOfLastSeparator(String filePath)
filePath - 文件路径public static void writeString(String content, String path, String charset) throws IOException
content - 写入的内容path - 文件路径charset - 字符集IOExceptionpublic static void appendString(String content, String path, String charset) throws IOException
content - 写入的内容path - 文件路径charset - 字符集IOExceptionpublic static String readString(String path, String charset) throws IOException
path - 文件路径charset - 字符集IOExceptionpublic static String readString(URL url, String charset) throws IOException
url - 文件URLcharset - 字符集IOExceptionpublic static <T> void writeLines(Collection<T> list, String path, String charset) throws IOException
list - 列表path - 绝对路径charset - 字符集IOExceptionpublic static <T> void appendLines(Collection<T> list, String path, String charset) throws IOException
list - 列表path - 绝对路径charset - 字符集IOExceptionpublic static <T> void writeLines(Collection<T> list, String path, String charset, boolean isAppend) throws IOException
list - 列表path - 绝对路径charset - 字符集isAppend - 是否追加IOExceptionpublic static void writeBytes(byte[] data,
String path)
throws IOException
data - 数据path - 目标文件IOExceptionpublic static void writeBytes(File dest, byte[] data) throws IOException
dest - 目标文件data - 数据IOExceptionpublic static void writeBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException
dest - 目标文件data - 数据off - len - append - IOExceptionpublic static byte[] readBytes(File file) throws IOException
file - 文件IOExceptionpublic static void writeStream(File dest, InputStream in) throws IOException
dest - 目标文件in - 输入流IOExceptionpublic static void writeStream(String fullFilePath, InputStream in) throws IOException
fullFilePath - 文件绝对路径in - 输入流IOExceptionpublic static boolean isModifed(File file, long lastModifyTime)
file - 文件对象lastModifyTime - 上次的改动时间Copyright © 2015. All rights reserved.