Copy the code code as follows:
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;
/**
* File operation tools
* @version 1.0 2013/07/16
*
*/
public class FileUtil {
/**
* Copy files or directories, and the files before and after copying will be exactly the same.
* @param resFilePath source file path
* @param distFolder target folder
* @IOException thrown when an exception occurs during the operation
*/
public static void copyFile(String resFilePath, String distFolder)
throws IOException {
File resFile = new File(resFilePath);
File distFile = new File(distFolder);
if (resFile.isDirectory()) { // Directory
FileUtils.copyDirectoryToDirectory(resFile, distFile);
} else if (resFile.isFile()) { // file
// FileUtils.copyFileToDirectory(resFile, distFile, true);
FileUtils.copyFileToDirectory(resFile, distFile);
}
}
/**
* Delete a file or directory
* @param targetPath file or directory path
* @IOException thrown when an exception occurs during the operation
*/
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();
}
}
/**
* Write the string into the specified file (when the folder in the specified parent path does not exist, it will be created as much as possible to ensure successful saving!)
*
* @param res original string
* @param filePath file path
* @return success mark
* @throwsIOException
*/
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()) {//Create when it does not exist
distFile.getParentFile().mkdirs();
}
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; //Character buffer
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;
}
/**
* Get the content of the specified file
*
* @param res original string
* @param filePath file path
* @return success mark
* @throwsIOException
*/
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;
}
/**
* Append content to the specified file
* @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;
}
}
}