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详解的更多相关文章

  1. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  2. JAVA IO 类库详解

    JAVA IO类库详解 一.InputStream类 1.表示字节输入流的所有类的超类,是一个抽象类. 2.类的方法 方法 参数 功能详述 InputStream 构造方法 available 如果用 ...

  3. 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 ...

  4. Apache的httpd命令详解

    Apache的httpd命令详解 来源:全栈开发者 发布时间:2012-01-03 阅读次数:10965 4   httpd.exe为Apache HTTP服务器程序.直接执行程序可启动服务器的服务. ...

  5. apache.commons.io.FileUtils的常用操作

    至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html package com.wz.apache.fileUtils; impo ...

  6. Apache Dolphin Scheduler - Dockerfile 详解

    Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeep ...

  7. apache的prefork的详解

    apache的prefork的参数详解:ServerLimit 2000 这是最大进程数的阀值StartServers 25  启动时建立的子进程MinSpareServers 25 最小空闲进程Ma ...

  8. iostat磁盘IO命令详解

    Linux IO 实时监控iostat命令详解 简介: 对于I/O-bond类型的进程,我们经常用iostat工具查看进程IO请求下发的数量.系统处理IO请求的耗时,进而分析进程与操作系统的交互过程中 ...

  9. Apache Spark 内存管理详解(转载)

    Spark 作为一个基于内存的分布式计算引擎,其内存管理模块在整个系统中扮演着非常重要的角色.理解 Spark 内存管理的基本原理,有助于更好地开发 Spark 应用程序和进行性能调优.本文旨在梳理出 ...

  10. 18、标准IO库详解及实例

    标准IO库是由Dennis Ritchie于1975年左右编写的,它是Mike Lestbain写的可移植IO库的主要修改版本,2010年以后, 标准IO库几乎没有进行什么修改.标准IO库处理了很多细 ...

随机推荐

  1. 为msysgit增加vim语法高亮文件

    在win7下装了msysgit,今天我遇到一个不爽的问题,打开git bash,用vim打开一个xml文件 结果都是黑屏的,没语法高亮,这个必须不能忍啊,我找到msysgit的安装目录,发现Vim73 ...

  2. svn 清空

    SVN是目前用得比较多的而且很方便的版本管理体系. 在开发过程中遇到了这样的问题: 有时我们需要一个干净的code版本,没有 .svn 这些文件夹记录的版本传到服务器上使用. 这个时候自己一个个去删除 ...

  3. 基于PinnedSectionListView实现联系人通讯录并且点击打电话

    PinnedSectionListView具体下载地址.使用方法和注意事项:http://www.cnblogs.com/zzw1994/p/4997601.html 怎么根据联系人姓名首字符顺序读取 ...

  4. Sqlite: unable to open database file

    A database connect, there updated both queries (different statement, and regardless of order), after ...

  5. Python GUI编程实践

    看完了<python编程实践>对Python的基本语法有了一定的了解,加上认识到python在图形用户界面和数据库支持方面快捷,遂决定动手实践一番. 因为是刚接触Python,对于基本的数 ...

  6. 驱动makefile

    1 ifeq ($(KERNELRELEASE),)  2 CURRENT_PATH=$(shell pwd)  3 #KERNEL_DIR:=/lib/modules/$(shell uname - ...

  7. MVC 删除文件

    protected void DeleteTempFiles(string iFileName) { FileInfo f = new FileInfo(iFileName); DirectoryIn ...

  8. JPA学习---第五节:日期和枚举等字段类型的JPA映射

    1.在上一节可在数据库中看到创建出来的表和字段,是通过 Entity bean 来创建的,而创建表名和字段名的规则是怎样的? 有类,代码如下: package learn.jpa.bean; impo ...

  9. android studio 智能提示忽略大小写

    Step1: Step2:

  10. SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束。

    SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束.