1.FileUtils介紹
檔案IO是我們日常專案中經常使用到的基礎API,常見的IO讀寫操作基礎類位元組流InputStream與OutputStream、字符流Reader與Writer已經涵蓋了我們日常專案開發中的常見API功能,具體的基礎回顧可以參見一篇cdsn博文:字符流與位元組流的區別
今天要說的,是我們基于上述API由Apache開源專案封裝的一個更好用的檔案操作工具類FileUtils,涵蓋了讀取檔案、拷貝檔案、拷貝目錄及檔案、洗掉目錄及檔案、清除目錄等比較常用的靜態類封裝,
FileUtils的使用需要引入apache的commons-io包下的FileUtils,匯入:import org.apache.commons.io.FileUtils;
官方API檔案:http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
maven專案使用需要匯入依賴:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
下面簡單介紹一下它常見的API方法的使用,
1.1.讀取檔案
官方API中我覺得能使用到的就是下面的方法,都是靜態方法,如readFileToString、readLines(按行讀取)等,對應的靜態方法也都提供了支持編碼"utf-8"格式讀取,

1.2.拷貝檔案
比較常用的API方法如下,如copyFile、copyInputStreamToFile、copyFileToDirectory,

①拷貝檔案到檔案 :copyFile(File srcFile, File destFile):
File file = new File("E:\\java\\file01\\abc雪.jpg"); String destFilePath = "E:\\java\\file02"; String destFileName = "abc雪02.jpg"; try { FileUtils fileUtils = new FileUtils(); //檔案拷貝到新的位置并保存檔案的日期, fileUtils.copyFile(file, new File(destFilePath,destFileName)); System.out.println("檔案拷貝成功"); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); }
②拷貝檔案到位元組輸出流:copyFile(File input, OutputStream output)
String destFileName = "abc雪03.jpg"; //從檔案copy to an位元組輸出流, FileUtils.copyFile(file, new FileOutputStream(new File(destFilePath,destFileName)));
③拷貝檔案到檔案的目錄保存檔案的日期:copyFileToDirectory(File srcFile, File destDir)
拷貝的檔案名無法自定義,和原檔案名一樣
//拷貝檔案到檔案的目錄保持檔案的日期, FileUtils.copyFileToDirectory(file, new File(destFilePath));

1.3.拷貝目錄及檔案
主要的API如下,包含拷貝目錄及目錄下的子目錄及檔案的拷貝方法:

①將整個目錄拷貝到新位置,并保持原檔案日期:copyDirectory(File srcDir, File destDir)
其包含檔案及子目錄檔案并保持原檔案日期
File file = new File("E:\\java\\file01"); String destFilePath = "E:\\java\\file03"; try { //將整個目錄復制新位置,并保持原檔案日期, FileUtils.copyDirectory(file, new File(destFilePath)); System.out.println("檔案目錄拷貝成功"); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); }

②將已篩選的目錄拷貝到新位置:
copyDirectory(File srcDir, File destDir, FileFilter filter)
檔案過濾器篩選其包含檔案及子目錄檔案拷貝,并保持原檔案日期,
String destFilePath = "E:\\java\\file04"; //將已篩選的目錄復制,并保持原檔案日期的新位置, FileUtils.copyDirectory(file, new File(destFilePath), new FileFilter() { @Override public boolean accept(File pathname) { if(pathname.isDirectory()) return true; else { boolean b1 = pathname.getName().endsWith(".txt"); boolean b2 = pathname.getName().endsWith(".jpg"); return b1 || b2; } } });

1.4.洗掉目錄及檔案
主要的洗掉API方法如下,常用的有deleteDirectory、deleteQuietly及forceDelete等,主要用于級聯洗掉及強制洗掉,且不會引起例外,非常強力,

①洗掉指定檔案,從不引發例外:deleteQuietly(File file)
File file = new File("E:\\java\\file04\\abc雪.jpg"); //洗掉指定檔案,從不引發例外, FileUtils.deleteQuietly(file);
②洗掉指定檔案,檔案不存在拋出例外:forceDelete(File file)
File file = new File("E:\\java\\file04\\abc雪.jpg"); try { FileUtils.forceDelete(file); System.out.println("操作成功"); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); }
③遞回洗掉目錄:deleteDirectory(File directory)
洗掉其包含檔案及子目錄檔案
File file = new File("E:\\java\\file04\\abc雪.jpg"); //遞回洗掉目錄, try { FileUtils.deleteDirectory(new File(destFilePath)); System.out.println("操作成功"); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); }
1.5.清除目錄
主要的API如下,主要用于清除當前目錄下(不會洗掉到當前目錄級別)的檔案夾及其包含的檔案,

清除該目錄下的檔案及子目錄檔案而不洗掉該目錄檔案夾,該目錄不存在會報錯:
String destFilePath = "E:\\java\\file04"; try { FileUtils.cleanDirectory(new File(destFilePath)); System.out.println("操作成功"); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); }
1.6.檔案大小計數常量
①EMPTY_FILE_ARRAY,空檔案陣列:
![]()
/** * An empty array of type {@code File}. */ public static final File[] EMPTY_FILE_ARRAY = {};
②1GB大小,分別對應long型別及BigInteger包裝型別:

/** * The number of bytes in a gigabyte. */ public static final long ONE_GB = ONE_KB * ONE_MB; /** * The number of bytes in a gigabyte. * * @since 2.4 */ public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
③同理KB與MB的顯示型別:

/** * The number of bytes in a kilobyte. */ public static final long ONE_KB = 1024; /** * The number of bytes in a kilobyte. * * @since 2.4 */ public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB); /** * The number of bytes in a megabyte. */ public static final long ONE_MB = ONE_KB * ONE_KB;
博文轉載至CSDN博客:
FileUtils工具類常用方法
基于博主博文我自己做了一小部分的知識補充,也非常感謝博主提供博文分享學習知識的平臺,
站在巨人的肩膀上,我們才能學到更多的知識~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/342999.html
標籤:Java
