FileUtils工具类的使用
http://blog.csdn.net/w369033345/article/details/53895259
writing to a file 写入文件
reading from a file 从文件中读取
make a directory including parentdirectories建立一个目录包括父目录
copying files and directories复制文件和目录
deleting files and directories删除文件和目录
converting to and from a URL转换到URL和从URL转换
listing files and directories by filter andextension通过滤波和扩展列表文件和目录
comparing file content比较文件内容
file last changed date文件最后更改日期
calculating a checksum计算校验和
1,获取系统临时目录
public static File getTempDirectory()
Returns:
thesystem temporary directory.
2,获取用户主目录
publicstatic File getUserDirectory()
Returns a File representingthe user's home directory.
Returns:
the user's homedirectory.
3,以可读的方式,返回文件的大小EB,PB, TB, GB, MB, KB or bytes
public static String byteCountToDisplaySize(BigInteger size)
public static String byteCountToDisplaySize(long size)
//
FileUtils.byteCountToDisplaySize(10000000);
FileUtils.byteCountToDisplaySize(1);
4,获取文件输入流
public static FileInputStream openInputStream(File file)
5,获取文件输出流,
public static FileOutputStream openOutputStream(File file)
InputStream in =FileUtils.openInputStream(new File("D://test/test1")); //D:/test也可以。
OutputStream out =FileUtils.openOutputStream(new File("D://test/test2"));
out = FileUtils.openOutputStream(newFile("D://test/test3"),true);//是否追加的形式添加内容
6,比较内容
public static boolean contentEquals(File file1, File file2)
此方法检查两个文件长度是否不同,或者是否指向同一个文件,然后再逐字节比较内容
public static boolean contentEqualsIgnoreEOL(File file1,
File file2,
String charsetName)
此方法检查两个文件是否指向同一个文件,然后再逐字节比较内容
FileUtils.contentEquals(newFile("D://test/test1"),new File("D://test/test2"));
FileUtils.contentEqualsIgnoreEOL(newFile("D://test/test1"),new File("D://test/test2"),null);//忽略换行符,第三个参数是字符集
7,
file - the file to write to
文件—要写入的文件
data - the content to write to the file
数据——写入文件的内容
off - the start offset in the data
数据中的起始偏移量
len - the number of bytes to write
要写入的字节数。
append - if true, then bytes will be addedto the end of the file rather than overwriting
如果如果是真的话,那么字节将被添加到文件末尾而不是重写