一、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,我們可以方便地處理文件和目錄操作,減少了手動實現的複雜度,使得編程變得更加高效、簡單。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/186244.html
微信掃一掃
支付寶掃一掃