JAVA工具类 -文件常用操作FileUtils
方便对目录管理,写入文件信息,读取文件信息。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class FileUtils {
private final static Logger logger = LoggerFactory.getLogger(FileUtils.class);
public static Boolean checkFileExist(String filepath) throws IOException {
File file = new File(filepath);
return file.exists();
}
public static File checkFileExistAndCreate(String filepath) throws IOException {
File file = new File(filepath);
if (!file.exists()) {// 判断文件的存在
file.createNewFile();// 创建文件
logger.info("文件不存在,创建文件成功!");
}
return file;
}
public static File checkDiretoryExist(String filepath) {
File file = new File(filepath);
if (!file.exists() && !file.isDirectory()) {// 判断文件目录的存在
logger.info("文件夹不存在,并创建:" + filepath);
file.mkdirs();
}
return file;
}
public static File createFileNotExist(String filepath) throws IOException {
File file = new File(filepath);
if (!file.exists()) {// 判断文件的存在
file.createNewFile();// 创建文件
logger.info("文件不存在,创建文件成功!");
}
return file;
}
public static File mkdirDiretoryNotExist(String filepath) {
File file = new File(filepath);
if (!file.exists() && !file.isDirectory()) {// 判断文件目录的存在
logger.info("文件夹不存在,并创建:" + filepath);
file.mkdirs();
}
return file;
}
public static File checkExist(String filepath) throws Exception {
File file = new File(filepath);
if (file.exists()) {// 判断文件目录的存在
System.out.println("文件夹存在!");
if (file.isDirectory()) {// 判断文件的存在性
System.out.println("文件存在!");
} else {
file.createNewFile();// 创建文件
System.out.println("文件不存在,创建文件成功!");
}
} else {
System.out.println("文件夹不存在!");
File file2 = new File(file.getParent());
file2.mkdirs();
System.out.println("创建文件夹成功!");
if (file.isDirectory()) {
System.out.println("文件存在!");
} else {
file.createNewFile();// 创建文件
System.out.println("文件不存在,创建文件成功!");
}
}
return file;
}
public static void writeStrToFile(String text, String filePath) {
if (text == null || filePath == null) {
return;
}
byte[] bts = text.getBytes();
File file = new File(filePath);
try {
if (!file.exists()) {// 判断文件的存在
file.createNewFile();// 创建文件
logger.info("文件不存在,创建文件成功!");
}
FileOutputStream in = new FileOutputStream(file);
in.write(bts);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readStrByFile(String filePath) {
if (filePath == null) {
return "";
}
File file = new File(filePath);
StringBuffer text = new StringBuffer("");
try {
if (!file.exists()) {// 判断文件的存在
logger.info("文件不存在,filePath:" + filePath);
}
FileReader rd = new FileReader(file);
BufferedReader bfr = new BufferedReader(rd);
int ch = 0;
while ((ch = bfr.read()) != -1) {
text.append(bfr.readLine() + "/r/n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return text.toString();
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean DeleteFolder(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) {
break;
}
} //删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) {
break;
}
}
if (!flag) {
return false;
}
}
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
}