以下代码整理自
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>