以下代碼整理自
https://www.cnblogs.com/vofill/p/6909780.html
https://blog.csdn.net/u011687186/article/details/51233968
本文只做個人工具記錄,不具有參考性,若要討論,請前往原帖
public class FileUtil {
/**
* @Fields log : log4j句柄
*/
protected static Logger log = Logger.getLogger(FileUtil.class);
/**
* @Title: readFile
* @Description: TODO(讀取文件的內容在指定的路徑下)
* @param @param path
* @param @return 設定文件
* @return String 返回類型
* @date 2016年4月24日 下午3:49:54
* @throws
*/
public static String readFile(String path) {
BufferedReader br = null;
String fileContent = "";
try {
br = new BufferedReader(new FileReader(new File(path)));
String temp = "";
while ((temp = br.readLine()) != null) {
fileContent = fileContent + temp;
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return fileContent;
}
/**
*
* @Title: readPakageFileUTF8
* @Description: TODO(讀取隨意某個包路徑下文件)
* @param @param filePath
* @param @return
* @param @throws Exception 設定文件
* @return String 返回類型
* @author: mxr
* @date 2016年4月22日 下午2:55:42
* @throws
*/
public static String readPakageFileUTF8(String filePath) {
String fileContent = "";
ClassLoader classLoader = FileUtil.class.getClassLoader();
// filePath 例如"com/sq/view/report/singleVehicle/action/chartbg.json"
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = classLoader.getResourceAsStream(filePath);
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
fileContent = fileContent + temp;
}
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (null != bufferedReader) {
try {
bufferedReader.close();
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return fileContent;
}
/**
* @Title: copyFile
* @Description: TODO(實現文本文件的複製 當isApend爲true時可以對文件內容進行追加)
* @param @param filePath
* @param @param outputPath
* @param @param isApend 設定文件
* @return void 返回類型
* @author: mxr
* @date 2016年4月24日 下午2:57:22
* @throws
*/
public static void copyFile(String filePath, String outputPath,
String content, boolean isApend) {
createNewDir(outputPath);
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File(filePath)));
bw = new BufferedWriter(new FileWriter(new File(outputPath),
isApend));
String str;// 結尾不等於空
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine(); // 重新起一行
bw.flush();
}
if (null != content && !"".equals(content)) {
bw.write(content);
bw.flush();
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (null != bw) {
try {
bw.close();
if (null != br) {
br.close();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
/**
* 可以實現對非文本文件的複製(例如:視頻,圖片,等流文件)
*
* @Title: copyStreamFile
* @Description: TODO(文件的複製)
* @param @param filePath
* @param @param outputPath
* @param @throws Exception 設定文件
* @return void 返回類型
* @author: mxr
* @date 2016年4月24日 下午2:50:33
* @throws
*/
public static void copyStreamFile(String filePath, String outputPath) {
createNewDir(filePath);
// 輸入流
BufferedInputStream bis = null;
// 輸出流
BufferedOutputStream bos = null;
try {
// 2.創建響應的節點流,FileInputStream,FileOutputStream
bis = new BufferedInputStream(new FileInputStream(
new File(filePath)));
bos = new BufferedOutputStream(new FileOutputStream(new File(
outputPath)));
// 實現文件的複製
int len = 0;
byte[] b = new byte[1024];// lKB=1024B
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
// BufferedOutputStream阻塞式的所以需要刷新
bos.flush();
}
} catch (Exception e) {
log.error(e.getMessage());
} finally {
// 關緩衝流就可以了,他會自動關閉牛節點流
if (null != bos) {
try {
bis.close();
if (null != bis) {
bis.close();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
/**
* @Title: writeContentFile
* @Description: TODO(寫入文件內容 isApend 爲 true 時 表示追加文件內容 爲false覆蓋文件內容)
* @param @param content
* @param @param path
* @param @param isApend
* @param @throws IOException 設定文件
* @return void 返回類型
* @author: mxr
* @date 2016年4月24日 下午3:05:26
* @throws
*/
public static void writeContentFile(String content, String path,
boolean isApend) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(path, isApend));
bw.write(content);
bw.flush();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
/**
* @Title: delFileOrDerectory
* @Description: TODO(刪除文件路徑下所有的文件)
* @param @param path 設定文件
* @return void 返回類型
* @author:mxr
* @date 2016年4月24日 下午3:12:02
* @throws
*/
public static void delFileOrDerectory(String path) {
try {
File file = new File(path);
if (file.exists()) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0, len = files.length; i < len; i++) {
File subFile = files[i];
delFileOrDerectory(subFile.getAbsolutePath());
}
file.delete();
} else {
file.delete();
}
}
} catch (Exception e) {
log.error("刪除文件時" + e.getMessage());
}
}
/**
* @Title: createNewDir
* @Description: TODO(如果文件不存在創建新的文件)
* @param @param outputPath 設定文件
* @return void 返回類型
* @author: mxr
* @date 2016年4月24日 下午2:29:40
* @throws
*/
public static void createNewDir(String outputPath) {
File outputFile = new File(outputPath);
File outputDir = outputFile.getParentFile();
if (!outputDir.exists()) {
outputDir.mkdirs();
}
}
/**
* 讀取某個文件夾下的所有文件
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
log.debug("文件");
log.debug("path=" + file.getPath());
log.debug("absolutepath=" + file.getAbsolutePath());
log.debug("name=" + file.getName());
} else if (file.isDirectory()) {
log.debug("文件夾");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
log.debug("path=" + readfile.getPath());
log.debug("absolutepath="
+ readfile.getAbsolutePath());
log.debug("name=" + readfile.getName());
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
log.debug("readfile() Exception:" + e.getMessage());
}
return true;
}
/**
* 刪除某個文件夾下的所有文件夾和文件
*/
public static boolean deletefile(String delpath)
throws FileNotFoundException, IOException {
try {
File file = new File(delpath);
if (!file.isDirectory()) {
log.debug("1");
file.delete();
} else if (file.isDirectory()) {
log.debug("2");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
log.debug("path=" + delfile.getPath());
log.debug("absolutepath="
+ delfile.getAbsolutePath());
log.debug("name=" + delfile.getName());
delfile.delete();
log.debug("刪除文件成功");
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
file.delete();
}
} catch (FileNotFoundException e) {
log.debug("deletefile() Exception:" + e.getMessage());
}
return true;
}
}
log4j及單元測試配置
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>