commons io文件操作示例分享
package com.pzq.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
/**
* 文件操作工具類
* @version 1.0 2013/07/16
*
*/
public class FileUtil {
/**
* 復(fù)制文件或者目錄,復(fù)制前后文件完全一樣。
* @param resFilePath 源文件路徑
* @param distFolder 目標(biāo)文件夾
* @IOException 當(dāng)操作發(fā)生異常時(shí)拋出
*/
public static void copyFile(String resFilePath, String distFolder)
throws IOException {
File resFile = new File(resFilePath);
File distFile = new File(distFolder);
if (resFile.isDirectory()) { // 目錄時(shí)
FileUtils.copyDirectoryToDirectory(resFile, distFile);
} else if (resFile.isFile()) { // 文件時(shí)
// FileUtils.copyFileToDirectory(resFile, distFile, true);
FileUtils.copyFileToDirectory(resFile, distFile);
}
}
/**
* 刪除一個(gè)文件或者目錄
* @param targetPath 文件或者目錄路徑
* @IOException 當(dāng)操作發(fā)生異常時(shí)拋出
*/
public static void deleteFile(String targetPath) throws IOException {
File targetFile = new File(targetPath);
if (targetFile.isDirectory()) {
FileUtils.deleteDirectory(targetFile);
} else if (targetFile.isFile()) {
targetFile.delete();
}
}
/**
* 將字符串寫入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)
*
* @param res 原字符串
* @param filePath 文件路徑
* @return 成功標(biāo)記
* @throws IOException
*/
public static boolean string2File(String res, String filePath) throws IOException {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists()) {// 不存在時(shí)創(chuàng)建
distFile.getParentFile().mkdirs();
}
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符緩沖區(qū)
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
flag = false;
throw e;
}
return flag;
}
/**
* 取得指定文件內(nèi)容
*
* @param res 原字符串
* @param filePath 文件路徑
* @return 成功標(biāo)記
* @throws IOException
*/
public static List<String> getContentFromFile(String filePath) throws IOException {
List<String> lists = null;
try {
if(!(new File(filePath).exists())){
return new ArrayList<String>();
}
lists = FileUtils.readLines(new File(filePath), Charset.defaultCharset());
} catch (IOException e) {
throw e;
}
return lists;
}
/**
* 給指定文件追加內(nèi)容
* @param filePath
* @param contents
*/
public static void addContent(String filePath, List<String> contents) throws IOException {
try {
FileUtils.writeLines(new File(filePath), contents);
} catch (IOException e) {
throw e;
}
}
}
相關(guān)文章
微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息
這篇文章主要介紹了微信企業(yè)號(hào)驗(yàn)證/發(fā)送/接收消息的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10Java飛行記錄器JFR功能實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Java飛行記錄器JFR功能實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05SpringBoot整合騰訊云COS對(duì)象存儲(chǔ)實(shí)現(xiàn)文件上傳的示例代碼
本文主要介紹了SpringBoot整合騰訊云COS對(duì)象存儲(chǔ)實(shí)現(xiàn)文件上傳的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12Java中Date與String相互轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了Java中Date與String相互轉(zhuǎn)換方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10如何解決SpringBoot2.x版本對(duì)Velocity模板不支持的方案
這篇文章主要介紹了如何解決SpringBoot2.x版本對(duì)Velocity模板不支持的方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12使用FeignClient調(diào)用遠(yuǎn)程服務(wù)時(shí)整合本地的實(shí)現(xiàn)方法
這篇文章主要介紹了使用FeignClient調(diào)用遠(yuǎn)程服務(wù)時(shí)整合本地的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java線程池對(duì)象ThreadPoolExecutor的深入講解
在我們的開發(fā)中“池”的概念并不罕見,有數(shù)據(jù)庫(kù)連接池、線程池、對(duì)象池、常量池等等。下面這篇文章主要給大家介紹了關(guān)于java線程池對(duì)象ThreadPoolExecutor的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09通過@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解
這篇文章主要介紹了通過@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Spring boot如何快速的配置多個(gè)Redis數(shù)據(jù)源
這篇文章主要介紹了Spring boot如何快速的配置多個(gè)Redis數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制的方法
spring-boot是一個(gè)快速的集成框架,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制,需要的朋友可以參考下2018-01-01