|
@@ -0,0 +1,138 @@
|
|
|
+package com.zkqy.common.utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+
|
|
|
+public class PlaceholderReplacer {
|
|
|
+
|
|
|
+ // 组件id占位符
|
|
|
+ private static final String PLACEHOLDEROne = "${componentId}";
|
|
|
+
|
|
|
+ // 组件ID
|
|
|
+ private static final String COMPONENT_ID = "3c374962955748cab96865977f608af6";
|
|
|
+
|
|
|
+ // 类描述信息占位符
|
|
|
+ private static final String PLACEHOLDERTwo="desc";
|
|
|
+
|
|
|
+ // 类描述信息
|
|
|
+ private static final String PLACEHOLDERDesc="组装节点";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ // 文件路径
|
|
|
+ String filePath = "D:\\zkqy\\test\\AddAmmunitionPickingUp.java";
|
|
|
+
|
|
|
+ // 新文件路径(基于组件ID生成)
|
|
|
+ String newFilePath = "D:\\zkqy\\test\\" + COMPONENT_ID + ".java";
|
|
|
+
|
|
|
+ // 第一步:将注解中的内容替换为占位符
|
|
|
+ String filledContent = replaceWithPlaceholder(filePath);
|
|
|
+
|
|
|
+ // 第二步:将填充好的内容写入新文件
|
|
|
+ createNewFile(newFilePath, filledContent);
|
|
|
+
|
|
|
+ System.out.println("操作完成!");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将注解中的内容替换为占位符
|
|
|
+ *
|
|
|
+ * @param filePath 文件路径
|
|
|
+ * @throws IOException 如果发生 I/O 错误
|
|
|
+ */
|
|
|
+ private static String replaceWithPlaceholder(String filePath) throws IOException {
|
|
|
+ // 读取文件内容
|
|
|
+ StringBuilder contentBuilder = new StringBuilder();
|
|
|
+ try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ if (line.contains("${componentId}")) {
|
|
|
+ // 替换为占位符
|
|
|
+ line = line.replace( PLACEHOLDEROne,COMPONENT_ID);
|
|
|
+ }
|
|
|
+ if(line.contains("@{desc}")){ //${desc}
|
|
|
+ line = line.replace(PLACEHOLDERTwo,PLACEHOLDERDesc ); //${desc}---> 组装节点
|
|
|
+ }
|
|
|
+ contentBuilder.append(line).append(System.lineSeparator());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// // 写回文件
|
|
|
+// try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath))) {
|
|
|
+// writer.write(contentBuilder.toString());
|
|
|
+// }
|
|
|
+ return contentBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重命名文件
|
|
|
+ *
|
|
|
+ * @param originalFilePath 原始文件路径
|
|
|
+ * @param newFileName 新文件名
|
|
|
+ * @throws IOException 如果发生 I/O 错误
|
|
|
+ */
|
|
|
+ private static void renameFile(String originalFilePath, String newFileName) throws IOException {
|
|
|
+ File originalFile = new File(originalFilePath);
|
|
|
+ File newFile = new File(originalFile.getParent(), newFileName);
|
|
|
+
|
|
|
+ // 确保新文件名不冲突
|
|
|
+ if (!newFile.exists()) {
|
|
|
+ boolean success = originalFile.renameTo(newFile);
|
|
|
+ if (!success) {
|
|
|
+ throw new IOException("无法重命名文件");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new IOException("目标文件已存在:" + newFile.getAbsolutePath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建新文件并将内容写入
|
|
|
+ *
|
|
|
+ * @param newFilePath 新文件路径
|
|
|
+ * @param content 要写入的内容
|
|
|
+ * @throws IOException 如果发生 I/O 错误
|
|
|
+ */
|
|
|
+ private static void createNewFile(String newFilePath, String content) throws IOException {
|
|
|
+ File newFile = new File(newFilePath);
|
|
|
+
|
|
|
+ // 确保父目录存在
|
|
|
+ if (!newFile.getParentFile().exists()) {
|
|
|
+ newFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入内容到新文件
|
|
|
+ try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(newFilePath))) {
|
|
|
+ writer.write(content);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取模板文件内容
|
|
|
+ *
|
|
|
+ * @param filePath 模板文件路径
|
|
|
+ * @return 文件内容
|
|
|
+ * @throws IOException 如果读取失败
|
|
|
+ */
|
|
|
+ public String readTemplateFile(String filePath) throws IOException {
|
|
|
+ // 读取文件内容
|
|
|
+ StringBuilder contentBuilder = new StringBuilder();
|
|
|
+ try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ contentBuilder.append(line).append(System.lineSeparator());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return contentBuilder.toString();
|
|
|
+ }
|
|
|
+}
|