commons-io
commons-io
2.4
FileUtils
/**
* writing to a file
* reading from a file
* make a directory including parent directories
* copying files and directories
* deleting files and directories
* converting to and from a URL
* listing files and directories by filter and extension
* comparing file content
* file last changed date
* calculating a checksum
*
* @version $Id: FileUtils.java 1349509 2012-06-12 20:39:23Z ggregory $
*/
public class FileUtils {
/**
* Instances should NOT be constructed in standard programming.
*/
public FileUtils() {
super();
}
/**
* 获取以directory/name[0]/name[1]/...为路径的文件实例
*/
public static File getFile(File directory, String... names) {
if (directory == null) {
throw new NullPointerException("directorydirectory must not be null");
}
if (names == null) {
throw new NullPointerException("names must not be null");
}
File file = directory;
for (String name : names) {
file = new File(file, name);
}
return file;
}
/**
* 获取以name[0]/name[1]/...为相对路径的文件实例
*/
public static File getFile(String... names) {
if (names == null) {
throw new NullPointerException("names must not be null");
}
File file = null;
for (String name : names) {
if (file == null) {
file = new File(name);
} else {
file = new File(file, name);
}
}
return file;
}
/**
* 获取系统的临时路径
*/
public static String getTempDirectoryPath() {
return System.getProperty("java.io.tmpdir");
}
public static File getTempDirectory() {
return new File(getTempDirectoryPath());
}
/**
* 获取用户的home目录
*/
public static String getUserDirectoryPath() {
return System.getProperty("user.home");
}
public static File getUserDirectory() {
return new File(getUserDirectoryPath());
}
/**
* 获取文件读取流,默认直接覆盖文件内容
*/
public static FileInputStream openInputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canRead() == false) {
throw new IOException("File '" + file + "' cannot be read");
}
} else {
throw new FileNotFoundException("File '" + file + "' does not exist");
}
return new FileInputStream(file);
}
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);
}
/**
* 将字节量转换为可读的容量
*/
public static String byteCountToDisplaySize(BigInteger size) {
String displaySize;
if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_EB_BI)) + " EB";
} else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_PB_BI)) + " PB";
} else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_TB_BI)) + " TB";
} else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_GB_BI)) + " GB";
} else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_MB_BI)) + " MB";
} else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = String.valueOf(size.divide(ONE_KB_BI)) + " KB";
} else {
displaySize = String.valueOf(size) + " bytes";
}
return displaySize;
}
public static String byteCountToDisplaySize(long size) {
return byteCountToDisplaySize(BigInteger.valueOf(size));
}
/**
* 创建空文件
*/
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 File[] convertFileCollectionToFileArray(Collection files) {
return files.toArray(new File[files.size()]);
}
private static void innerListFiles(Collection files, File directory,
IOFileFilter filter, boolean includeSubDirectories) {
File[] found = directory.listFiles((FileFilter) filter);
if (found != null) {
for (File file : found) {
if (file.isDirectory()) {
if (includeSubDirectories) {
files.add(file);
}
innerListFiles(files, file, filter, includeSubDirectories);
} else {
files.add(file);
}
}
}
}
/**
* 查询目录下的所有文件,并可用filter进行过滤
*/
public static Collection listFiles(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
validateListFilesParameters(directory, fileFilter);
IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
//Find files
Collection files = new java.util.LinkedList();
innerListFiles(files, directory,
FileFilterUtils.or(effFileFilter, effDirFilter), false);
return files;
}
public static Collection listFilesAndDirs(
File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
validateListFilesParameters(directory, fileFilter);
IOFileFilter effFileFilter = setUpEffectiveFileFilter(fileFilter);
IOFileFilter effDirFilter = setUpEffectiveDirFilter(dirFilter);
//Find files
Collection files = new java.util.LinkedList();
if (directory.isDirectory()) {
files.add(directory);
}
innerListFiles(files, directory,
FileFilterUtils.or(effFileFilter, effDirFilter), true);
return files;
}
//... 对文件夹/文件的遍历、复制、读、写、删除等操作
}