一、FileUtils概述
FileUtils是Apache Commons IO工具库的一部分,提供了许多封装好的静态方法来操作文件和文件夹,减少了我们手动实现的复杂度。通过使用FileUtils,我们可以方便地复制、移动、删除、重命名文件,以及追加/替换文件内容等操作。
二、FileUtils的安装
安装FileUtils十分简单,只需要将commons-io包导入项目的classpath中即可。Maven依赖如下:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
三、FileUtils的使用
1、读取文件
我们可以使用FileUtils.readFileToString()方法从文件中读取字符串,该方法可以指定文件的编码方式。在方法调用失败时抛出异常。
public static String readFileToString(File file, String encoding) throws IOException {
return readLines(file, encoding).toString();
}
实例:
File file = new File("file.txt");
String content = FileUtils.readFileToString(file, "UTF-8");
System.out.println(content);
2、写入文件
我们可以使用FileUtils.write()方法将字符或字符串写入到文件中,该方法会自动创建文件以及相关的目录。如果你想使用追加模式将数据写入现有文件,则可以使用FileUtils.write()方法,并将第三个参数设置为true。
public static void write(File file, CharSequence data, Charset encoding, boolean append) throws IOException {
String str = data == null? null: data.toString();
writeStringToFile(file, str, encoding, append);
}
实例:
String content = "Hello, World!";
File file = new File("file.txt");
FileUtils.write(file, content, "UTF-8", false);
// 追加模式
FileUtils.write(file,content,"UTF-8",true);
3、复制文件
我们可以使用FileUtils.copyFile()方法复制文件。如果文件已经存在,则其被覆盖。如果源文件和目标文件是同一个文件,则不执行任何操作并返回。
public static void copyFile(File srcFile, File destFile) throws IOException {
if (srcFile == null) {
throw new IllegalArgumentException("Source must not be null");
}
if (destFile == null) {
throw new IllegalArgumentException("Destination must not be null");
}
if (!srcFile.exists()) {
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
}
copyFile(srcFile, destFile, true);
}
实例:
File srcFile = new File("src/file.txt");
File destFile = new File("dest/file.txt");
FileUtils.copyFile(srcFile,destFile);
4、移动文件
我们可以使用FileUtils.moveFile()方法将文件移动到新位置。如果目标文件存在,则源文件将覆盖其内容。如果源文件和目标文件是同一个文件,则不执行任何操作并返回。
public static void moveFile(File srcFile, File destFile) throws IOException {
if (srcFile == null) {
throw new IllegalArgumentException("Source must not be null");
}
if (destFile == null) {
throw new IllegalArgumentException("Destination must not be null");
}
if (!srcFile.exists()) {
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
}
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' is a directory");
}
if (destFile.exists()) {
if (!destFile.canWrite()) {
throw new IOException("Destination '" + destFile + "' is read-only");
}
if (!confirmOverwrite(destFile)) {
return;
}
} else {
File parentDir = destFile.getParentFile();
if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Destination '" + parentDir + "' directory cannot be created");
}
}
if (!srcFile.renameTo(destFile)) {
copyFile(srcFile, destFile);
if (!srcFile.delete()) {
FileUtils.deleteQuietly(destFile);
throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'");
}
}
}
实例:
File srcFile = new File("src/file.txt");
File destFile = new File("dest/file.txt");
FileUtils.moveFile(srcFile,destFile);
5、删除文件
我们可以使用FileUtils.delete()方法删除文件或目录以及其所有子目录。该方法递归地删除目录中的所有子目录和文件。如果删除失败,则在方法结束时抛出异常。
public static void delete(File file) throws IOException {
if (file.isDirectory()) {
cleanDirectory(file);
}
if (!file.delete()) {
throw new IOException("Unable to delete file: " + file);
}
}
实例:
File file = new File("file.txt");
FileUtils.deleteQuietly(file);
四、总结
通过本篇文章的介绍,我们了解了FileUtils的概述,安装步骤以及常用方法的使用。使用FileUtils,我们可以方便地处理文件和目录操作,减少了手动实现的复杂度,使得编程变得更加高效、简单。