package com.github.hunter0x7c7.sync.utils;
|
|
import java.io.*;
|
|
public class FileUtil {
|
|
|
/**
|
* 使用 BufferedWriter 写文件
|
*
|
* @param filepath 文件目录
|
* @param content 待写入内容
|
*/
|
public static boolean bufferedWriter(String filepath, String content) {
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filepath))) {
|
bw.write(content);
|
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
|
private String readFromInputStream(InputStream is) throws IOException {
|
StringBuilder sb = new StringBuilder();
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
|
String line;
|
while ((line = br.readLine()) != null) {
|
sb.append(line).append("\n");
|
}
|
}
|
return sb.toString();
|
}
|
|
public static String bufferedReader(String file) {
|
StringBuilder sb = new StringBuilder();
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
String line;
|
while ((line = reader.readLine()) != null) {
|
sb.append(line).append("\n");
|
}
|
reader.close();
|
} catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return sb.toString();
|
}
|
|
public static boolean copyFile(String oldPath, String newPath) {
|
return copyFile(new File(oldPath), newPath);
|
}
|
|
public static boolean copyFile(File oldFile, String newPath) {
|
try {
|
if (oldFile != null && oldFile.exists()) {
|
InputStream inStream = new FileInputStream(oldFile);
|
FileOutputStream fs = new FileOutputStream(newPath);
|
byte[] buffer = new byte[1024 * 2];
|
|
// int byteSum = 0;
|
int byteRead;
|
while ((byteRead = inStream.read(buffer)) != -1) {
|
// byteSum += byteRead;
|
fs.write(buffer, 0, byteRead);
|
}
|
fs.flush();
|
fs.close();
|
inStream.close();
|
}
|
|
return true;
|
} catch (Exception var7) {
|
var7.printStackTrace();
|
System.out.println("复制单个文件操作出错");
|
return false;
|
}
|
}
|
|
public static boolean copyFolder(String oldPath, String newPath) {
|
try {
|
boolean mkdirs = (new File(newPath)).mkdirs();
|
File a = new File(oldPath);
|
String[] file = a.list();
|
|
for (int i = 0; i < file.length; ++i) {
|
File temp;
|
if (oldPath.endsWith(File.separator)) {
|
temp = new File(oldPath + file[i]);
|
} else {
|
temp = new File(oldPath + File.separator + file[i]);
|
}
|
|
if (temp.isFile()) {
|
FileInputStream input = new FileInputStream(temp);
|
FileOutputStream output = new FileOutputStream(newPath + "/" + temp.getName());
|
byte[] b = new byte[5120];
|
|
int len;
|
while ((len = input.read(b)) != -1) {
|
output.write(b, 0, len);
|
}
|
|
output.flush();
|
output.close();
|
input.close();
|
}
|
|
if (temp.isDirectory()) {
|
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
|
}
|
}
|
|
return true;
|
} catch (Exception var11) {
|
System.out.println("复制整个文件夹内容操作出错");
|
var11.printStackTrace();
|
return false;
|
}
|
}
|
|
|
/**
|
* 删除文件,可以是文件或文件夹
|
*
|
* @param fileName:要删除的文件名
|
* @return 删除成功返回true,否则返回false
|
*/
|
public static boolean delete(String fileName) {
|
File file = new File(fileName);
|
if (!file.exists()) {
|
System.out.println("删除文件失败:" + fileName + "不存在!");
|
return false;
|
} else {
|
if (file.isFile()) {
|
return deleteFile(fileName);
|
} else {
|
return deleteDirectory(fileName);
|
}
|
}
|
}
|
|
/**
|
* 删除单个文件
|
*
|
* @param fileName:要删除的文件的文件名
|
* @return 单个文件删除成功返回true,否则返回false
|
*/
|
public static boolean deleteFile(String fileName) {
|
File file = new File(fileName);
|
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
if (file.exists() && file.isFile()) {
|
if (file.delete()) {
|
System.out.println("删除单个文件" + fileName + "成功!");
|
return true;
|
} else {
|
System.out.println("删除单个文件" + fileName + "失败!");
|
return false;
|
}
|
} else {
|
System.out.println("删除单个文件失败:" + fileName + "不存在!");
|
return false;
|
}
|
}
|
|
/**
|
* 删除目录及目录下的文件
|
*
|
* @param dir:要删除的目录的文件路径
|
* @return 目录删除成功返回true,否则返回false
|
*/
|
public static boolean deleteDirectory(String dir) {
|
// 如果dir不以文件分隔符结尾,自动添加文件分隔符
|
if (!dir.endsWith(File.separator)) {
|
dir = dir + File.separator;
|
}
|
File dirFile = new File(dir);
|
// 如果dir对应的文件不存在,或者不是一个目录,则退出
|
if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
|
System.out.println("删除目录失败:" + dir + "不存在!");
|
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 if (files[i].isDirectory()) {
|
flag = deleteDirectory(files[i].getAbsolutePath());
|
if (!flag) {
|
break;
|
}
|
}
|
}
|
if (!flag) {
|
System.out.println("删除目录失败!");
|
return false;
|
}
|
// 删除当前目录
|
if (dirFile.delete()) {
|
System.out.println("删除目录" + dir + "成功!");
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
}
|