FileUtils文件处理操作
本帖最后由 Maybe 于 2024-6-21 17:20 编辑 准备工作:
1.下载jar 地址:http://commons.apache.org/proper/commons-io/download_io.cgi
2.把commons-io-2.4.jar 这个文件导入到你的项目中
说明:
1.由于是一个工具类使用都非常的简单 所以本文只是将其分类,展示它能够提供给我们什么。
2.习惯看api的可以直接看官方的api传送门
3.可以看一看官方的指引指引传送门
4.FileUtils只是commons-io的其中一个工具类
分类说明演示:
1.写 文件/文件夹/* 写文件
* 1.这里只列出3种方式全参数形式,api提供部分参数的方法重载
* 2.最后一个布尔参数都是是否是追加模式
* 3.如果目标文件不存在,FileUtils会自动创建
* */
//static void:write(File file, CharSequence data, String encoding, boolean append)
FileUtils.write(new File("D:/a/b/cxyapi.txt"), "程序换api","UTF-8",true);
//static void:writeLines(File file, Collection<?> lines, boolean append)
List<String> lines=new ArrayList<String>();
lines.add("欢迎访问:");lines.add("www.cxyapi.com");
FileUtils.writeLines(new File("D:/a/b/cxyapi.txt"),lines,true);
//static void:writeStringToFile(File file, String data, String encoding, boolean append)
FileUtils.writeStringToFile(new File("D:/a/b/cxyapi.txt"), "作者:cxy", "UTF-8",true);
data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
2.读 文件/文件夹//读文件
//static String:readFileToString(File file, String encoding)
System.out.println(FileUtils.readFileToString(new File("D:/a/b/cxyapi.txt"), "UTF-8"));
//static List<String>:readLines(File file, String encoding)
System.out.println(FileUtils.readLines(new File("D:/a/b/cxyapi.txt"), "UTF-8")); //返回一个list
data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
3.删除 文件/文件夹
//删除目录
//static void:deleteDirectory(File directory)
FileUtils.deleteDirectory(new File("D:/not/cxyapi"));
//static boolean:deleteQuietly(File file)
FileUtils.deleteQuietly(new File("D:/not/cxyapi")); //文件夹不是空任然可以被删除,永远不会抛出异常
data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
4.移动 文件/文件夹
//移动文件 或 文件夹
//static void:moveDirectory(File srcDir, File destDir)
FileUtils.moveDirectory(new File("D:/cxyapi1"), new File("D:/cxyapi2")); //注意这里 第二个参数文件不存在会引发异常
//static void:moveDirectoryToDirectory(File src, File destDir, boolean createDestDir)
FileUtils.moveDirectoryToDirectory(new File("D:/cxyapi2"), new File("D:/cxyapi3"), true);
/* 上面两个方法的不同是:
* moveDirectory:D:/cxyapi2里的内容是D:/cxyapi1的内容。
* moveDirectoryToDirectory:D:/cxyapi2文件夹移动到到D:/cxyapi3里
*
* 下面的3个都比较简单没提供示例,只提供了api
* 其中moveToDirectory和其他的区别是 它能自动识别操作文件还是文件夹
*/
//static void:moveFileToDirectory(srcFile, destDir, createDestDir)
//static void:moveFile(File srcFile, File destFile)
//static void:moveToDirectory(File src, File destDir, boolean createDestDir)
data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
5.copy//结果是cxyapi和cxyapi1在同一目录
FileUtils.copyDirectory(new File("D:/cxyapi"), new File("D:/cxyapi1"));
//结果是将cxyapi拷贝到cxyapi2下
FileUtils.copyDirectoryToDirectory(new File("D:/cxyapi"), new File("D:/cxyapi2"));
//拷贝文件
FileUtils.copyFile(new File("d:/cxyapi.xml"), new File("d:/cxyapi.xml.bak"));
//拷贝文件到目录中
FileUtils.copyFileToDirectory(new File("d:/cxyapi.xml"), new File("d:/cxyapi"));
//拷贝url到文件
FileUtils.copyURLToFile(new URL("http://www.cxyapi.com/rss/cxyapi.xml"), new File("d:/cxyapi.xml")); data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
6.其他
//判断是否包含文件或者文件夹
boolean b=FileUtils.directoryContains(new File("D:/cxyapi"), new File("D:/cxyapi/cxyapi.txt"));
System.out.println(b);
//获得临时目录 和 用户目录
System.out.println(FileUtils.getTempDirectoryPath());
System.out.println(FileUtils.getUserDirectoryPath());
//打开流,如果不存在创建文件及其目录结构
//第二个参数表示 文件流是否是追加方式
FileOutputStream fos=FileUtils.openOutputStream(new File("D:/cxyapi/cxyapi.txt"),true);
fos.write(new String("欢迎访问:www.cxyapi.com\r\n").getBytes());
fos.close();
//文件 或 文件夹大小
System.out.println(FileUtils.sizeOf(new File("D:/cxyapi")));
System.out.println(FileUtils.sizeOfDirectory(new File("D:/cxyapi")));
data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
7bit casino online real money e143mi
Nicely put, Appreciate it!
best cryptologic online casino best online poker sites for real money live online casino canada
mejores casino online c27dbp
Fantastic postings. Thank you.
top online casinos 2023 casino games roulette baccarat online casino games
online casino vpn friendly betting mlb bally online casino promo code
are online casinos legal in georgia bet us super bowl odds atlantic city casino games online
best dragonfish online casino top bitcoin casino go wild online casino
online casino download slots online free games die besten online casinos 2020
online casino deposit 1 get 20 m928an
Thanks. Loads of write ups!
slotomania online casino best online casino arizona 7 melons online casino
cheapest online casino online craps online casino 5 euro einzahlung
online casino mit google pay bezahlen nhl live betting ontario casino online
vegas online casino bonus code real money casino app no deposit canada best online casino
10 min online casino for usa 2017 horse race betting ruleta casino juego online
asia city online casino o70zti
Edithpab ??? 2024-11-3 05:24Fantastic postings. Thank you.
top online casinos 2023 casino games roulette baccarat online casi ...
Nicely put. With thanks!
online casino games no deposit free play soccer betting online agen betting oriental casino online
newest west virginia online casino best online casino in australia rich palms online casino
real. time. gaming. online. casinos. baccarat online real money fair go online casino no deposit codes
south african casino online arizona online casino real money crazywinners online casino
free spin online casino no deposit bonus codes betting on nba players casino online real money australia
list of online casino t603un
Edithpab ??? 2024-10-12 12:50Nicely put, Appreciate it!
best cryptologic online casino best online poker sites for real money...
Perfectly spoken of course! !
2022 online casinos real money poker online casino online ticket booking
crazy online casino ignition casino reviews usa online casino slots
firekeepers online casino free spins online casino games casino online sin deposito
spinland online casino review online golf betting real online casinos usa
city of dreams online casino app ducky luck no deposit bonus luigi casino game online
gta online casino heist elite challenge rewards m163qw
Amazing info. With thanks!
echtgeld casino online best online poker rooms nj online casino promo code
online casino d online casino real money colorado real money online casino indiana no deposit
fire phoenix casino game online free online casino australia real money gun lake casino online app
casino spel online mybookie promo code online casino games slots
aud online casinos mybookie reviews funclub online casino
century casino online j43rbw
Edithpab ??? 2024-11-3 15:33Amazing info. With thanks!
echtgeld casino online best online poker rooms nj online casino promo...
Thanks a lot, I value it!
casino online minimum deposit 5 sportsbook betting tropicana las vegas online casino promo code
online casino chargeback reddit nhl betting online play real money online casino
online casino credit online casino australia real money real online casino slot games
slots of vegas online casino instant play are online casinos safe online casinos free money no deposit
online casinos accepting discover card nfl betting 2017 usa online casino
online casino offering cashback on losses h318ju
Thank you. Ample posts.
riverbelle online casino free download nfl sports betting list of online casinos usa
bet online casino reviews play online omaha poker monarch online casino instant play
win win casino online real money casinos online snai casino online
casino games online with friends best mma betting site online casino games chumba casino login
20 best online casinos washington dc casino online online casino malaysia apk
rapid transfer online casino i143et
You made your point quite well..
australian online casino fast payout casino online casinos texas
tropicana pa online casino app real casino app rp888 online casino
online casinos for usa table tennis bets online casinos with fluffy favourites
michigan online casino slots xbet promo code free play all slot online casino
best alaska online casino casino online canada 2022 no deposit bonus codes online casinos