|
|
@@ -1,15 +1,13 @@
|
|
|
package cn.reghao.autodop.common.utils;
|
|
|
|
|
|
import cn.reghao.autodop.common.utils.text.FileProcessor;
|
|
|
-import cn.reghao.autodop.common.utils.text.TextFile;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.file.*;
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* 文件,目录操作类(新建,删除,复制,移动,清空)
|
|
|
@@ -20,86 +18,30 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
public class FileOps {
|
|
|
/**
|
|
|
- * TODO: 复制文件或目录
|
|
|
+ * 将目录的文件和子目录复制到另一个目录(非当前目录或其子目录)
|
|
|
*
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2019-08-27 下午10:37
|
|
|
- */
|
|
|
- public static boolean cp(String src, String dst) throws IOException {
|
|
|
- if (src.equals(dst)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- File srcFile = new File(src);
|
|
|
- File dstFile = new File(dst);
|
|
|
- copyFile(srcFile, dstFile);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- public static boolean copyFile(String src, String dst) throws IOException {
|
|
|
- if (src.equals(dst)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- File srcFile = new File(src);
|
|
|
- File dstFile = new File(dst);
|
|
|
- if (!srcFile.exists() || srcFile.isDirectory()) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (dstFile.isFile()) {
|
|
|
- copyFile(srcFile, dstFile);
|
|
|
- } else if (!dstFile.exists()) {
|
|
|
- if (!dstFile.createNewFile()) {
|
|
|
- log.info("创建 {} 失败...", dst);
|
|
|
- return false;
|
|
|
- }
|
|
|
- copyFile(srcFile, dstFile);
|
|
|
- } else if (dstFile.isDirectory()) {
|
|
|
- log.info("not implementation");
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 移动目录
|
|
|
- *
|
|
|
- * @param
|
|
|
+ * @param src 要复制的目录
|
|
|
+ * @param dst 目的目录(若不存在则新建)
|
|
|
* @return
|
|
|
- * @date 2019-09-10 下午5:52
|
|
|
+ * @date 2019-09-09 下午6:11
|
|
|
*/
|
|
|
- public static void mvDir(String src, String dst) throws IOException {
|
|
|
+ public static void copyDirContentToDir(String src, String dst) throws IOException {
|
|
|
File srcDir = new File(src);
|
|
|
- File dstDir = new File(dst);
|
|
|
-
|
|
|
if (!srcDir.exists() || srcDir.isFile()) {
|
|
|
- log.info("{} 不存在", src);
|
|
|
+ log.error("{} -> 源目录不存在", src);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- FileUtils.moveDirectoryToDirectory(srcDir, dstDir, true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将目录的内容复制到一个新位置
|
|
|
- *
|
|
|
- * @param src 要复制内容的目录
|
|
|
- * @param dst 目的目录(若不存在则新建)
|
|
|
- * @return
|
|
|
- * @date 2019-09-09 下午6:11
|
|
|
- */
|
|
|
- public static void copyDir(String src, String dst) throws IOException {
|
|
|
- File srcFile = new File(src);
|
|
|
- if (!srcFile.exists() || srcFile.isFile()) {
|
|
|
- log.error("{} -> 目录不存在", src);
|
|
|
- return;
|
|
|
+ File dstDir = new File(dst);
|
|
|
+ for (File file : Objects.requireNonNull(srcDir.listFiles())) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ String dirname = file.getName();
|
|
|
+ File dstdir = new File(dst + "/" + dirname);
|
|
|
+ FileUtils.copyDirectory(file, dstdir);
|
|
|
+ } else {
|
|
|
+ FileUtils.copyFileToDirectory(file, dstDir);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- File dstFile = new File(dst);
|
|
|
- FileUtils.copyDirectory(srcFile, dstFile);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -121,83 +63,6 @@ public class FileOps {
|
|
|
FileUtils.copyDirectory(srcDir, dstDir);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 复制文件
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2019-09-06 下午11:19
|
|
|
- */
|
|
|
- private static void copyFile(File src, File dst) throws IOException {
|
|
|
- InputStream in = new BufferedInputStream(new FileInputStream(src));
|
|
|
- OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
|
|
|
-
|
|
|
- byte[] bytes = new byte[1024];
|
|
|
- int size;
|
|
|
- while ((size = in.read(bytes)) != -1) {
|
|
|
- out.write(bytes, 0, size);
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 抛出异常时处理未关闭的流
|
|
|
- in.close();
|
|
|
- out.close();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 移动文件到目录或文件
|
|
|
- *
|
|
|
- * @param src 文件绝对路径
|
|
|
- * @param dst 文件或目录绝对路径,若 dst 不存在,则默认其为文件
|
|
|
- * @return
|
|
|
- * @date 2019-08-27 下午9:52
|
|
|
- */
|
|
|
- public static boolean mvFile(String src, String dst) throws IOException {
|
|
|
- File srcFile = new File(src);
|
|
|
- File dstFile = new File(dst);
|
|
|
-
|
|
|
- if (src.equals(dst) || !srcFile.exists() || srcFile.isDirectory()) {
|
|
|
- log.info("{} 移动失败...", src);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (dstFile.exists()) {
|
|
|
- if (dstFile.isFile()) {
|
|
|
- writeFile(srcFile, dstFile);
|
|
|
- } else {
|
|
|
- // 移动文件到目录
|
|
|
- String newPath = dst + "/" + srcFile.getName();
|
|
|
- File newFile = new File(newPath);
|
|
|
- srcFile.renameTo(newFile);
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- if (!dstFile.createNewFile()) {
|
|
|
- log.info("{} 创建失败...", dst);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- writeFile(srcFile, dstFile);
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static void writeFile(File src, File dst) throws IOException {
|
|
|
- BufferedReader reader;
|
|
|
- BufferedWriter writer;
|
|
|
- reader = new BufferedReader(new FileReader(src));
|
|
|
- writer = new BufferedWriter(new FileWriter(dst));
|
|
|
-
|
|
|
- String line;
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
- writer.write(line + System.lineSeparator());
|
|
|
- }
|
|
|
-
|
|
|
- // TODO: 处理关闭时的异常
|
|
|
- reader.close();
|
|
|
- writer.close();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 创建目录,若目录存在则返回
|
|
|
*
|
|
|
@@ -226,32 +91,6 @@ public class FileOps {
|
|
|
return dir.exists() && dir.isDirectory();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 删除目录及其包含的所有子目录或文件
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2019-08-27 下午10:38
|
|
|
- */
|
|
|
- public static boolean rmDir(String src) {
|
|
|
- File file = new File(src);
|
|
|
- if (!file.exists() || file.isFile()) {
|
|
|
- log.info("目录不存在 -> {}", src);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (file.isDirectory()) {
|
|
|
- try {
|
|
|
- FileUtils.deleteDirectory(file);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 提取一个绝对路径中的目录部分
|
|
|
*
|
|
|
@@ -264,29 +103,6 @@ public class FileOps {
|
|
|
return path.substring(0, index);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 文件指纹
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2019-08-28 下午1:21
|
|
|
- */
|
|
|
- public static Map<String, String> fingerPrint(String filePath) {
|
|
|
- File file = new File(filePath);
|
|
|
- if (!file.exists()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, String> res = new HashMap<>();
|
|
|
- if (!file.isDirectory()) {
|
|
|
- res.put(filePath, null);
|
|
|
- } else {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return res;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 遍历并处理文本文件
|
|
|
*
|
|
|
@@ -327,23 +143,6 @@ public class FileOps {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 清空目录
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2019-09-27 下午3:20
|
|
|
- */
|
|
|
- public static void eraseDir(String dirPath) throws IOException {
|
|
|
- File dir = new File(dirPath);
|
|
|
- if (!dir.exists() || dir.isFile()) {
|
|
|
- log.info("{} -> 目录不存在", dirPath);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- FileUtils.cleanDirectory(dir);
|
|
|
- }
|
|
|
-
|
|
|
public static void eraseDir(File dir) throws IOException {
|
|
|
if (!dir.exists() || dir.isFile()) {
|
|
|
log.info("{} -> 目录不存在", dir.getPath());
|
|
|
@@ -352,21 +151,4 @@ public class FileOps {
|
|
|
|
|
|
FileUtils.cleanDirectory(dir);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 新建一个文件
|
|
|
- *
|
|
|
- * @param
|
|
|
- * @return
|
|
|
- * @date 2020-03-10 上午10:36
|
|
|
- */
|
|
|
- public static void newFile(String filepath, String filecontent) throws IOException {
|
|
|
- File file = new File(filepath);
|
|
|
- if (!file.exists() && !file.createNewFile()) {
|
|
|
- log.info("创建 {} 失败", filepath);
|
|
|
- }
|
|
|
-
|
|
|
- /*TextFile.empty(file);
|
|
|
- TextFile.write(file, filecontent);*/
|
|
|
- }
|
|
|
}
|