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ā)生異常時拋出
*/
public static void copyFile(String resFilePath, String distFolder)
throws IOException {
File resFile = new File(resFilePath);
File distFile = new File(distFolder);
if (resFile.isDirectory()) { // 目錄時
FileUtils.copyDirectoryToDirectory(resFile, distFile);
} else if (resFile.isFile()) { // 文件時
// FileUtils.copyFileToDirectory(resFile, distFile, true);
FileUtils.copyFileToDirectory(resFile, distFile);
}
}
/**
* 刪除一個文件或者目錄
* @param targetPath 文件或者目錄路徑
* @IOException 當(dāng)操作發(fā)生異常時拋出
*/
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)指定的父路徑中文件夾不存在時,會最大限度去創(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()) {// 不存在時創(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)文章
SpringBoot整合騰訊云COS對象存儲實現(xiàn)文件上傳的示例代碼
本文主要介紹了SpringBoot整合騰訊云COS對象存儲實現(xiàn)文件上傳的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12Java中Date與String相互轉(zhuǎn)換的方法
這篇文章主要為大家詳細介紹了Java中Date與String相互轉(zhuǎn)換方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10如何解決SpringBoot2.x版本對Velocity模板不支持的方案
這篇文章主要介紹了如何解決SpringBoot2.x版本對Velocity模板不支持的方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12使用FeignClient調(diào)用遠程服務(wù)時整合本地的實現(xiàn)方法
這篇文章主要介紹了使用FeignClient調(diào)用遠程服務(wù)時整合本地的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03java線程池對象ThreadPoolExecutor的深入講解
在我們的開發(fā)中“池”的概念并不罕見,有數(shù)據(jù)庫連接池、線程池、對象池、常量池等等。下面這篇文章主要給大家介紹了關(guān)于java線程池對象ThreadPoolExecutor的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-09-09Spring boot如何快速的配置多個Redis數(shù)據(jù)源
這篇文章主要介紹了Spring boot如何快速的配置多個Redis數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06spring-boot整合ehcache實現(xiàn)緩存機制的方法
spring-boot是一個快速的集成框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了spring-boot整合ehcache實現(xiàn)緩存機制,需要的朋友可以參考下2018-01-01