org.apache.common.io-FileUtils详解
org.apache.common.io---FileUtils详解
getTempDirectoryPath():返回临时目录路径;
public static String getTempDirectoryPath() {
return System.getProperty("java.io.tmpdir");
}
getTempDirectory():返回临时目录文件路径的File实例;
public static File getTempDirectory() {
return new File(getTempDirectoryPath());
}
getUserDirectoryPath():返回用户目录路径;
public static String getUserDirectoryPath() {
return System.getProperty("user.home");
}
getUserDirectory():返回用户目录路径的File实例
public static File getUserDirectory() {
return new File(getUserDirectoryPath());
}
openInputStream(File file):通过指定文件创建一个输入流;如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
public static FileOutputStream openOutputStream(File file) throws IOException {
return openOutputStream(file, false);
}
public static FileOutputStream openOutputStream(File file, boolean append) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
}
return new FileOutputStream(file, append);
}
byteCountToDisplaySize(long size):返回指定数值的计算机表示(KB,MB,GB);
/**
* The number of bytes in a kilobyte.
*/
public static final long ONE_KB = 1024; /**
* The number of bytes in a megabyte.
*/
public static final long ONE_MB = ONE_KB * ONE_KB;
/**
* The number of bytes in a gigabyte.
*/
public static final long ONE_GB = ONE_KB * ONE_MB;
public static String byteCountToDisplaySize(long size) {
String displaySize; // if (size / ONE_EB > 0) {
// displaySize = String.valueOf(size / ONE_EB) + " EB";
// } else if (size / ONE_PB > 0) {
// displaySize = String.valueOf(size / ONE_PB) + " PB";
// } else if (size / ONE_TB > 0) {
// displaySize = String.valueOf(size / ONE_TB) + " TB";
// } else
if (size / ONE_GB > 0) {
displaySize = String.valueOf(size / ONE_GB) + " GB";
} else if (size / ONE_MB > 0) {
displaySize = String.valueOf(size / ONE_MB) + " MB";
} else if (size / ONE_KB > 0) {
displaySize = String.valueOf(size / ONE_KB) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
touch(File file):更新指定文件最终修改时间和访问时间;若文件不存在则生成空文件;Closeable 是可以关闭的数据源或目标。调用 close 方法可释放对象保存的资源(如打开文件)。
public static void touch(File file) throws IOException {
if (!file.exists()) {
OutputStream out = openOutputStream(file);
IOUtils.closeQuietly(out);
}
boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}
public static void closeQuietly(OutputStream output) {
closeQuietly((Closeable)output);
}
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ioe) {
// ignore
}
}
convertFileCollectionToFileArray(Collection<File> files):把文件集合转换为数组形式;
public static File[] convertFileCollectionToFileArray(Collection<File> files) {
return files.toArray(new File[files.size()]);
}
contentEquals(File file1, File file2):比较两个文件内容是否相同;两个文件都不存在表示相同;不能比较目录,会抛出异常;
public static boolean contentEquals(File file1, File file2) throws IOException {
boolean file1Exists = file1.exists();
if (file1Exists != file2.exists()) {
return false;
}
if (!file1Exists) {
// two not existing files are equal
return true;
}
if (file1.isDirectory() || file2.isDirectory()) {
// don't want to compare directory contents
throw new IOException("Can't compare directories, only files");
}
if (file1.length() != file2.length()) {
// lengths differ, cannot be equal
return false;
}
if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
// same file
return true;
}
InputStream input1 = null;
InputStream input2 = null;
try {
input1 = new FileInputStream(file1);
input2 = new FileInputStream(file2);
return IOUtils.contentEquals(input1, input2);
} finally {
IOUtils.closeQuietly(input1);
IOUtils.closeQuietly(input2);
}
}
copyFileToDirectory(File srcFile, File
destDir),copyFileToDirectory(File srcFile, File destDir, boolean
preserveFileDate):拷贝源文件到指定目录;如果目标目录不存在,则会被创建;如果目标文件存在,源文件会把它覆盖;如果第三个参数为
true,设置拷贝的目标文件最终修改时间与源文件时间一样;
public static void copyFileToDirectory(File srcFile, File destDir) throws IOException {
copyFileToDirectory(srcFile, destDir, true);
}
copyFile(File input, OutputStream output):把一个文件写到输出流里;返回写入的字节数大小;
public static long copyFile(File input, OutputStream output) throws IOException {
final FileInputStream fis = new FileInputStream(input);
try {
return IOUtils.copyLarge(fis, output);
} finally {
fis.close();
}
}
copyDirectory(File srcDir, File destDir),copyDirectory(File srcDir, File destDir,
boolean preserveFileDate),copyDirectory(File srcDir, File destDir,
FileFilter filter, boolean preserveFileDate):拷贝源目录及目录下所有文件到指定目录,第三个参数如果为true,设置拷贝后的目录最后修改时间与源目录时间相同
filter为过滤后的文件进行拷贝;
public static void copyDirectory(File srcDir, File destDir) throws IOException {
copyDirectory(srcDir, destDir, true);
}
copyInputStreamToFile(InputStream source, File destination):把输入流中的内容写到指定文件中;
deleteDirectory(File directory):删除指定目录文件及目录下所有内容;
cleanDirectory(File directory):清空指定目录文件下所有内容,但不删除目录;
boolean deleteQuietly(File file):删除指定目录文件及目录下所有内容;但不抛出异常,异常在内部已经捕获;
readFileToString(File file, String encoding),readFileToString(File file):读取指定文件内容到一个字符串;第二个参数为指定的字符集编码;
readFileToByteArray(File file):读取指定文件到字节数组;
readLines(File file, String encoding):读取指定文件按行存入字符串List,第二个参数为指定的字符集编码;
writeStringToFile(File
file, String data, String encoding),writeStringToFile(File file, String
data, String encoding, boolean
append):按指定的编码把字符串写入指定文件中;第四个参数如果为true,则把内容写到文件最后;如果文件不存在则创建;
writeByteArrayToFile(File file, byte[] data),writeByteArrayToFile(File file, byte[] data, boolean append):同上;
forceDelete(File file):删除指定文件,如果为目录,则清空目录并删除目录文件,如果为文件,直接删除文件;
forceDeleteOnExit(File file):在JVM退出时进行删除,如果为目录,则清空目录并删除目录文件,如果为文件,直接删除文件;
cleanDirectoryOnExit(File directory) :在JVM退出时清空目录;
forceMkdir(File directory):创建指定目录,如果失败抛出异常;
sizeOf(File file):返回指定文件大小或者目录下所有文件大小之和;
isFileNewer(File file, File reference):判断给定文件与比较文件哪个更新(创建时间更晚),第二个参数为参照文件;
isFileOlder(File file, File reference):判断给定文件与比较文件哪个更旧(创建时间更早),第二个参数为参照文件;
moveDirectory(File srcDir, File destDir):将源目录移动为指定目录;重命名那句代码,如果生命名成功就无须再移动文件了,如果生命名失败再进行拷贝和删除操作;
public static void moveDirectory(File srcDir, File destDir) throws IOException {
if (srcDir == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination must not be null");
}
if (!srcDir.exists()) {
throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
}
if (!srcDir.isDirectory()) {
throw new IOException("Source '" + srcDir + "' is not a directory");
}
if (destDir.exists()) {
throw new FileExistsException("Destination '" + destDir + "' already exists");
}
boolean rename = srcDir.renameTo(destDir);
if (!rename) {
copyDirectory( srcDir, destDir );
deleteDirectory( srcDir );
if (srcDir.exists()) {
throw new IOException("Failed to delete original directory '" + srcDir +
"' after copy to '" + destDir + "'");
}
}
}
moveDirectoryToDirectory(File src, File destDir, boolean createDestDir):把源目录移动到指定目录下,如果目标目录不存在,根据第三个参数是否创建;如果不存在并且不创建,则会抛出异常;
moveFile(File srcFile, File destFile):移动文件,同上;
moveFileToDirectory(File srcFile, File destDir, boolean createDestDir):移动文件到指定目录;如果目标目录不存在,根据第三个参数是否创建;如果不存在并且不创建,则会抛出异常;
org.apache.common.io-FileUtils详解的更多相关文章
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- JAVA IO 类库详解
JAVA IO类库详解 一.InputStream类 1.表示字节输入流的所有类的超类,是一个抽象类. 2.类的方法 方法 参数 功能详述 InputStream 构造方法 available 如果用 ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
1.错误叙述性说明 警告: Could not create JarEntryRevision for [jar:file:/D:/MyEclipse/apache-tomcat-7.0.53/web ...
- Apache的httpd命令详解
Apache的httpd命令详解 来源:全栈开发者 发布时间:2012-01-03 阅读次数:10965 4 httpd.exe为Apache HTTP服务器程序.直接执行程序可启动服务器的服务. ...
- apache.commons.io.FileUtils的常用操作
至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html package com.wz.apache.fileUtils; impo ...
- Apache Dolphin Scheduler - Dockerfile 详解
Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeep ...
- apache的prefork的详解
apache的prefork的参数详解:ServerLimit 2000 这是最大进程数的阀值StartServers 25 启动时建立的子进程MinSpareServers 25 最小空闲进程Ma ...
- iostat磁盘IO命令详解
Linux IO 实时监控iostat命令详解 简介: 对于I/O-bond类型的进程,我们经常用iostat工具查看进程IO请求下发的数量.系统处理IO请求的耗时,进而分析进程与操作系统的交互过程中 ...
- Apache Spark 内存管理详解(转载)
Spark 作为一个基于内存的分布式计算引擎,其内存管理模块在整个系统中扮演着非常重要的角色.理解 Spark 内存管理的基本原理,有助于更好地开发 Spark 应用程序和进行性能调优.本文旨在梳理出 ...
- 18、标准IO库详解及实例
标准IO库是由Dennis Ritchie于1975年左右编写的,它是Mike Lestbain写的可移植IO库的主要修改版本,2010年以后, 标准IO库几乎没有进行什么修改.标准IO库处理了很多细 ...
随机推荐
- 分享:perl 文件操作总结
发布:thebaby 来源:net [大 中 小] perl 文件操作,包括打开.关闭文件,读取.定入文件等.原文链接:http://www.jbxue.com/article/3153.html 打 ...
- 深入理解JavaScript的变量作用域(转载Rain Man之作)
在学习JavaScript的变量作用域之前,我们应当明确几点: JavaScript的变量作用域是基于其特有的作用域链的. JavaScript没有块级作用域. 函数中声明的变量在整个函数中都有定义. ...
- 如何重建Octopress本地环境
# 安装rvm, ruby, bundler 略 # 克隆octopress $ git clone git://github.com/imathis/octopress.git octopress ...
- Oracle中的注释
注释用于对程序代码的解释说明,它能够增强程序的可读性,是程序易于理解. 单行注释: 用“--”,后面跟上注释的内容 Declare Num_sal number; --声明一个数字类型的变量 Var_ ...
- 【转载】MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法
原文地址:MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法 作者:mchdba 某个初级dba误删index,mysql漫山遍野全是10S以上的慢sql,mysql ...
- Go在linux下的安装
在Ubuntu.Debian 或者 Linux Mint上安装Go语言 下面是在基于Debian的发行版上使用apt-get来安装Go语言和它的开发工具. $ sudo apt-get install ...
- iOS的动画效果类型及实现方法
实现iOS漂亮的动画效果主要有两种方法, 一种是UIView层面的, 一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransit ...
- 微软职位内部推荐-SDE2 (Windows - Power)
微软近期Open的职位: SDE2 (Windows - Power) Windows Partner Enablement team in Operating System Group is loo ...
- Version of SQLite used in Android?
sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...
- 初见IOS的UI之:UI控件的属性frame bounds center 和transform
这些属性,内部都是结构体:CGRect CGPoint CGFloat 背景知识:所有的控件都是view的子类,屏幕就是一个大的view:每个view都有个viewController,它是view的 ...