com.xiaoleilu.hutool
类 FileUtil

java.lang.Object
  继承者 com.xiaoleilu.hutool.FileUtil

public class FileUtil
extends Object

文件工具类

作者:
xiaoleilu

嵌套类摘要
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 cleanDir(String dirPath)
          清空一个目录
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>>
T
readLines(String path, String charset, T collection)
          从文件中读取每一行数据
static List<String> readLines(URL url, String charset)
          从文件中读取每一行数据
static
<T extends Collection<String>>
T
readLines(URL url, String charset, T collection)
          从文件中读取每一行数据
static String readString(String path, 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写入文件,覆盖模式
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

CLASS_EXT

public static final String CLASS_EXT
Class文件扩展名

另请参见:
常量字段值

JAR_FILE_EXT

public static final String JAR_FILE_EXT
Jar文件扩展名

另请参见:
常量字段值

JAR_PATH_EXT

public static final String JAR_PATH_EXT
在Jar中的路径jar的扩展名形式

另请参见:
常量字段值

PATH_FILE_PRE

public static final String PATH_FILE_PRE
当Path为文件形式时, path会加入一个表示文件的前缀

另请参见:
常量字段值
构造方法详细信息

FileUtil

public FileUtil()
方法详细信息

ls

public static File[] ls(String path)
列出目录文件
给定的绝对路径不能是压缩包中的路径

参数:
path - 目录绝对路径或者相对路径
返回:
文件列表(包含目录)

listFileNames

public static List<String> listFileNames(String path)
获得指定目录下所有文件
不会扫描子目录

参数:
path - 相对ClassPath的目录或者绝对路径目录
返回:
文件路径列表(如果是jar中的文件,则给定类似.jar!/xxx/xxx的路径)
抛出:
IOException

touch

public static File touch(String fullFilePath)
                  throws IOException
创建文件,如果这个文件存在,直接返回这个文件

参数:
fullFilePath - 文件的全路径,使用POSIX风格
返回:
文件,若路径为null,返回null
抛出:
IOException

del

public static boolean del(String fullFileOrDirPath)
                   throws IOException
删除文件或者文件夹

参数:
fullFileOrDirPath - 文件或者目录的路径
返回:
成功与否
抛出:
IOException

del

public static boolean del(File file)
                   throws IOException
删除文件或者文件夹

参数:
file - 文件对象
返回:
成功与否
抛出:
IOException

mkdir

public static File mkdir(String dirPath)
创建文件夹,如果存在直接返回此文件夹

参数:
dirPath - 文件夹路径,使用POSIX格式,无论哪个平台
返回:
创建的目录

createTempFile

public static File createTempFile(String prefix,
                                  String suffix,
                                  File dir,
                                  boolean isReCreat)
                           throws IOException
创建临时文件
创建后的文件名为 prefix[Randon].suffix From com.jodd.io.FileUtil

参数:
prefix - 前缀,至少3个字符
suffix - 后缀,如果null则使用默认.tmp
dir - 临时文件创建的所在目录
isReCreat - 是否重新创建文件(删掉原来的,创建新的)
返回:
临时文件
抛出:
IOException

copy

public static void copy(File src,
                        File dest,
                        boolean isOverride)
                 throws IOException
复制文件
如果目标文件为目录,则将源文件以相同文件名拷贝到目标目录

参数:
src - 源文件
dest - 目标文件或目录
isOverride - 是否覆盖目标文件
抛出:
IOException

move

public static void move(File src,
                        File dest,
                        boolean isOverride)
                 throws IOException
移动文件或者目录

参数:
src - 源文件或者目录
dest - 目标文件或者目录
isOverride - 是否覆盖目标
抛出:
IOException

getAbsolutePath

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

参数:
path - 相对路径
baseClass - 相对路径所相对的类
返回:
绝对路径

getAbsolutePath

public static String getAbsolutePath(String path)
获取绝对路径,相对于classes的根目录
如果给定就是绝对路径,则返回原路径,原路径把所有\替换为/

参数:
path - 相对路径
返回:
绝对路径

isExist

public static boolean isExist(String path)
文件是否存在

参数:
path - 文件路径
返回:
是否存在

close

public static void close(Closeable closeable)
关闭

参数:
closeable - 被关闭的对象

equals

public static boolean equals(File file1,
                             File file2)
检查两个文件是否是同一个文件

参数:
file1 - 文件1
file2 - 文件2
返回:
是否相同

getBufferedWriter

public static BufferedWriter getBufferedWriter(String path,
                                               String charset,
                                               boolean isAppend)
                                        throws IOException
获得一个带缓存的写入对象

参数:
path - 输出路径,绝对路径
charset - 字符集
isAppend - 是否追加
返回:
BufferedReader对象
抛出:
IOException

getPrintWriter

public static PrintWriter getPrintWriter(String path,
                                         String charset,
                                         boolean isAppend)
                                  throws IOException
获得一个打印写入对象,可以有print

参数:
path - 输出路径,绝对路径
charset - 字符集
isAppend - 是否追加
返回:
打印对象
抛出:
IOException

getOutputStream

public static OutputStream getOutputStream(String path)
                                    throws IOException
获得一个输出流对象

参数:
path - 输出到的文件路径,绝对路径
返回:
输出流对象
抛出:
IOException

cleanDir

public static void cleanDir(String dirPath)
清空一个目录

参数:
dirPath - 需要删除的文件夹路径

getReader

public static BufferedReader getReader(String path,
                                       String charset)
                                throws IOException
获得一个文件读取器

参数:
path - 绝对路径
charset - 字符集
返回:
BufferedReader对象
抛出:
IOException

readLines

public static <T extends Collection<String>> T readLines(String path,
                                                         String charset,
                                                         T collection)
                                              throws IOException
从文件中读取每一行数据

参数:
path - 文件路径
charset - 字符集
collection - 集合
返回:
文件中的每行内容的集合
抛出:
IOException

readLines

public static <T extends Collection<String>> T readLines(URL url,
                                                         String charset,
                                                         T collection)
                                              throws IOException
从文件中读取每一行数据

参数:
url - 文件的URL
charset - 字符集
collection - 集合
返回:
文件中的每行内容的集合
抛出:
IOException

readLines

public static List<String> readLines(URL url,
                                     String charset)
                              throws IOException
从文件中读取每一行数据

参数:
url - 文件的URL
charset - 字符集
返回:
文件中的每行内容的集合List
抛出:
IOException

readLines

public static List<String> readLines(String path,
                                     String charset)
                              throws IOException
从文件中读取每一行数据

参数:
path - 文件路径
charset - 字符集
返回:
文件中的每行内容的集合List
抛出:
IOException

load

public static <T> T load(FileUtil.ReaderHandler<T> readerHandler,
                         String path,
                         String charset)
              throws IOException
按照给定的readerHandler读取文件中的数据

参数:
readerHandler - Reader处理类
path - 文件的绝对路径
charset - 字符集
返回:
从文件中load出的数据
抛出:
IOException

getExtension

public static String getExtension(String fileName)
获得文件的扩展名

参数:
fileName - 文件名
返回:
扩展名

indexOfLastSeparator

public static int indexOfLastSeparator(String filePath)
获得最后一个文件路径分隔符的位置

参数:
filePath - 文件路径
返回:
最后一个文件路径分隔符的位置

writeString

public static void writeString(String content,
                               String path,
                               String charset)
                        throws IOException
将String写入文件,覆盖模式

参数:
content - 写入的内容
path - 文件路径
charset - 字符集
抛出:
IOException

appendString

public static void appendString(String content,
                                String path,
                                String charset)
                         throws IOException
将String写入文件,追加模式

参数:
content - 写入的内容
path - 文件路径
charset - 字符集
抛出:
IOException

readString

public static String readString(String path,
                                String charset)
                         throws IOException
读取文件内容

参数:
path - 文件路径
charset - 字符集
返回:
内容
抛出:
IOException

writeLines

public static <T> void writeLines(Collection<T> list,
                                  String path,
                                  String charset)
                       throws IOException
将列表写入文件,覆盖模式

参数:
list - 列表
path - 绝对路径
charset - 字符集
抛出:
IOException

appendLines

public static <T> void appendLines(Collection<T> list,
                                   String path,
                                   String charset)
                        throws IOException
将列表写入文件,追加模式

参数:
list - 列表
path - 绝对路径
charset - 字符集
抛出:
IOException

writeLines

public static <T> void writeLines(Collection<T> list,
                                  String path,
                                  String charset,
                                  boolean isAppend)
                       throws IOException
将列表写入文件

参数:
list - 列表
path - 绝对路径
charset - 字符集
isAppend - 是否追加
抛出:
IOException

writeBytes

public static void writeBytes(byte[] data,
                              String path)
                       throws IOException
写数据到文件中

参数:
data - 数据
path - 目标文件
抛出:
IOException

writeBytes

public static void writeBytes(File dest,
                              byte[] data)
                       throws IOException
写数据到文件中

参数:
dest - 目标文件
data - 数据
抛出:
IOException

writeBytes

public static void writeBytes(File dest,
                              byte[] data,
                              int off,
                              int len,
                              boolean append)
                       throws IOException
写入数据到文件

参数:
dest - 目标文件
data - 数据
off -
len -
append -
抛出:
IOException

readBytes

public static byte[] readBytes(File file)
                        throws IOException
读取文件所有数据
文件的长度不能超过Integer.MAX_VALUE

参数:
file - 文件
返回:
字节码
抛出:
IOException

writeStream

public static void writeStream(File dest,
                               InputStream in)
                        throws IOException
将流的内容写入文件

参数:
dest - 目标文件
in - 输入流
抛出:
IOException

writeStream

public static void writeStream(String fullFilePath,
                               InputStream in)
                        throws IOException
将流的内容写入文件

参数:
fullFilePath - 文件绝对路径
in - 输入流
抛出:
IOException

isModifed

public static boolean isModifed(File file,
                                long lastModifyTime)
判断文件是否被改动
如果文件对象为 null 或者文件不存在,被视为改动

参数:
file - 文件对象
lastModifyTime - 上次的改动时间
返回:
是否被改动


Copyright © 2014. All rights reserved.