若依项目名、包名修改器
【代码】若依项目名、包名修改器。
·
Updater.java
import java.io.File;
import java.util.*;
public class Updater {
public static void main(String[] args) {
String projectPath="D:\\workspace\\idea\\RuoYi-Cloud-Plus-2.X";
updateNames(new File(projectPath));
}
//需要改名的目录名称(包含即可)
static Map<String,String> reNameDirMap=new LinkedHashMap<>();
static {
reNameDirMap.put("ruoyi","zyym");
reNameDirMap.put("RuoYi","Zyym");
reNameDirMap.put("RUOYI","ZYYM");
reNameDirMap.put("ruoYi","zyym");
reNameDirMap.put("dromara","zyym");
reNameDirMap.put("Dromara","Zyym");
reNameDirMap.put("DROMARA","ZYYM");
}
//需要改名的父级目录名称(org.dromara这种,改org为com)
static Map<String,String> reNameDirMapByParent=new LinkedHashMap<>();
static {
reNameDirMapByParent.put("org","com");
}
//需要改名的文件名称(包含即可)
static Map<String,String> reNameFileMap=new LinkedHashMap<>();
static {
reNameFileMap.put("ruoyi","zyym");
reNameFileMap.put("RuoYi","Zyym");
reNameFileMap.put("RUOYI","ZYYM");
reNameFileMap.put("ruoYi","zyym");
reNameFileMap.put("dromara","zyym");
reNameFileMap.put("Dromara","Zyym");
reNameFileMap.put("DROMARA","ZYYM");
reNameFileMap.put("ry-","zyym-");
reNameFileMap.put("_ry_","_zyym_");
}
//哪些文件后缀需要修改内容
static List<String> reContentExtList=new ArrayList<>();
static {
reContentExtList.add(".java");
reContentExtList.add(".vue");
reContentExtList.add(".js");
reContentExtList.add(".ts");
reContentExtList.add(".json");
reContentExtList.add(".html");
reContentExtList.add(".css");
reContentExtList.add(".scss");
reContentExtList.add(".yml");
reContentExtList.add(".yaml");
reContentExtList.add(".sql");
reContentExtList.add(".xml");
reContentExtList.add(".LoadBalance");
reContentExtList.add(".vm");
reContentExtList.add(".factories");
reContentExtList.add(".imports");
reContentExtList.add(".Filter");
reContentExtList.add(".properties");
reContentExtList.add("Dockerfile");
reContentExtList.add(".md");
}
//需要改内容的关键词
static Map<String,String> reContentKeyMap=new LinkedHashMap<>();
static {
reContentKeyMap.put("org.dromara","com.zyym");
reContentKeyMap.put("ruoyi","zyym");
reContentKeyMap.put("RuoYi","Zyym");
reContentKeyMap.put("RUOYI","ZYYM");
reContentKeyMap.put("ruoYi","zyym");
reContentKeyMap.put("dromara","zyym");
reContentKeyMap.put("Dromara","Zyym");
reContentKeyMap.put("DROMARA","ZYYM");
reContentKeyMap.put("com.zyym.sms4j","org.dromara.sms4j");//还原不需要替换的sms4j
reContentKeyMap.put("com.zyym.easy","org.dromara.easy");//还原不需要替换的easy
}
//需要改内容的关键词(sql专用)
static Map<String,String> reSqlKeyMap=new LinkedHashMap<>();
static {
reSqlKeyMap.put("ry-","zyym-");
}
//需要改内容的关键词(yml和properties专用)
static Map<String,String> reYmlKeyMap=new LinkedHashMap<>();
static {
reYmlKeyMap.put("/ry-","/zyym-");
}
//改名
public static void updateNames(File file) {
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
boolean exist=false;
for (int i = 0; i < listFiles.length; i++) {
if(reNameDirMap.containsKey(listFiles[i].getName())&&listFiles[i].isDirectory()){
exist=true;
}
updateNames(listFiles[i]);
}
if(exist&&reNameDirMapByParent.containsKey(file.getName())){
//修改名称org为com
file.renameTo(new File(file.getParentFile().getAbsolutePath()+File.separator+reNameDirMapByParent.get(file.getName())));
}else{
//修改名称ruoyi-xxx为zyym-xxx等各种包含的
Set<String> keys = reNameDirMap.keySet();
for (String key:keys) {
if(file.getName().contains(key)){
String newName = file.getName().replace(key, reNameDirMap.get(key));
File newFile=new File(file.getParentFile().getAbsolutePath()+File.separator+newName);
file.renameTo(newFile);
if(!newFile.exists()){
FileUtil.moveFile(newFile,file);
}
break;
}
}
}
}else if(file.isFile()){
//修改名称ruoyi-xxx为zyym-xxx等各种包含的
Set<String> keys = reNameFileMap.keySet();
for (String key:keys) {
if(file.getName().contains(key)){
String newName = file.getName().replace(key, reNameFileMap.get(key));
File newFile=new File(file.getParentFile().getAbsolutePath()+File.separator+newName);
file.renameTo(newFile);
if(!newFile.exists()){
FileUtil.moveFile(newFile,file);
}
file=newFile;
break;
}
}
updateContent(file);
}
}
//改内容
public static void updateContent(File file) {
//判断是否需要修改内容
String suffiex = FileUtil.getSuffiex(file.getName());
for (String s :reContentExtList) {
if(file.getName().endsWith(s)){
String content = FileUtil.readTextFile(file);
for (String k:reContentKeyMap.keySet()) {
if(content.contains(k)){
content=content.replaceAll(k,reContentKeyMap.get(k));
}
}
if(".sql".equals(suffiex)){
for (String k:reSqlKeyMap.keySet()) {
if(content.contains(k)){
content=content.replaceAll(k,reSqlKeyMap.get(k));
}
}
}
if(".yml".equals(suffiex)||".properties".equals(suffiex)){
for (String k:reYmlKeyMap.keySet()) {
if(content.contains(k)){
content=content.replaceAll(k,reYmlKeyMap.get(k));
}
}
}
//替换内容并保存
FileUtil.saveTextFile(file,content);
break;
}
}
}
}
FileUtil.java
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* 文件相关工具类
*
* @author tang
*/
public class FileUtil {
/**
* 判断文件大小是否合乎指定大小,如果超出,返回false,否则返回true
* @param file
* @param size_MB
* @return
*/
public static boolean checkFileSize(File file, int size_MB) {
long size = size_MB * 1024 * 1024;
return file.length() > size ? false : true;
}
public static String makeNewFileName(String originalFileName) {
return System.currentTimeMillis() + String.format("%03d", new Random().nextInt(1000)) + originalFileName.substring(originalFileName.indexOf("."));
}
/**
* 修改文件后缀
* @param filePath
* @param newPostfix 文件后缀不带'.'
* @return
*/
public static String resetPostfix(String filePath, String newPostfix) {
String targetPath;
int point_index = filePath.lastIndexOf(".");
if (point_index != -1) {
targetPath = filePath.substring(0, point_index) + "." + newPostfix;
} else {
targetPath = filePath + "." + newPostfix;
}
return targetPath;
}
/**
* 获取文件所在的文件夹路径
* @param filePath
* @return
*/
public static String getFolderPath(String filePath) {
return new File(filePath).getParent();
}
/**
* 获取文件后缀名,不包括'.'
* @param filePath
* @return
*/
public static String getPostfix(String filePath) {
String name=new File(filePath).getName();
int lastIndexOf = name.lastIndexOf(".");
if (lastIndexOf >= 0) {
return name.substring(lastIndexOf + 1);
}
return "";
}
/**
* 获取文件后缀名,包括'.'
* @param filePath
* @return
*/
public static String getSuffiex(String filePath) {
String name=new File(filePath).getName();
int lastIndexOf = name.lastIndexOf(".");
if (lastIndexOf >= 0) {
return name.substring(lastIndexOf);
}
return "";
}
/**
* 获取文件名,不包括文件后缀名
* @param filePath
* @return
*/
public static String getPureName(String filePath) {
File file=new File(filePath);
String name = file.getName();
int lastIndexOf = name.lastIndexOf(".");
if (lastIndexOf >= 0) {
return name.substring(0, lastIndexOf);
}
return name;
}
public static String getFileName(String filePath) {
File file=new File(filePath);
String name = file.getName();
return name;
}
/**
* 文件是否存在
* @param file
* @return
*/
public static boolean exists(File file) {
return file != null && file.exists();
}
/**
* 文件是否能写
* @param targetFile
* @return
*/
public static boolean isCanWrite(File targetFile) {
if (targetFile == null) {
return false;
}
if (targetFile.isDirectory()) {
return false;
}
if (!targetFile.exists()) {
return true;
}
try (FileOutputStream fos = new FileOutputStream(targetFile, true)) {
return true;
} catch (Exception e) {
return false;
}
}
public static String readTextFile(File file) {
return readTextFile(file, "UTF-8");
}
public static String readTextFile(File file, String code) {
try {
return new String(Files.readAllBytes(file.toPath()), code);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建一个文件或文件夹,自动创建不存在的上级目录,如果文件或文件夹存在则不创建,
* @param filePath
* @return
*/
public static File createFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
return file;
}
if (file.isDirectory()) {
file.mkdirs();
return file;
}
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
/**
* 保存字符串到一个文本文件(UTF-8),如果文件不存在则创建
* @param filePath
* @param content 文件内容
*/
public static void saveTextFile(String filePath, String content) {
saveTextFile(new File(filePath), content);
}
/**
* 保存字符串到一个文本文件(UTF-8),如果文件不存在则创建
* @param file
* @param content 文件内容
*/
public static void saveTextFile(File file, String content) {
saveTextFile(file, content, "UTF-8");
}
/**
* 保存字符串到一个文本文件,如果文件不存在则创建
* @param file
* @param content 文件内容
* @param code 文件内容编码
*/
public static void saveTextFile(File file, String content, String code) {
try {
deleteFile(file);//不删除的话,写入的内容可能会莫名其妙多出一些东西,原因未知
Files.write(file.toPath(), content.getBytes(code), StandardOpenOption.CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除一个文件或文件夹(递归)
* @param file
*/
public static void deleteFile(File file) {
if (!exists(file)) {
return;
}
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for (int i = 0; i < listFiles.length; i++) {
deleteFile(listFiles[i]);
}
// Files.delete(file.toPath());
file.delete();
} else {
file.delete();
}
}
/**
* 移动一个文件或文件夹(递归)
*/
public static void moveFile(File sourceFile, File targetFile) {
if (!exists(sourceFile)) {
return;
}
if (sourceFile.isDirectory()) {
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File[] listFiles = sourceFile.listFiles();
for (int i = 0; i < listFiles.length; i++) {
File childTargetFile = new File(targetFile.getPath() + File.separator + listFiles[i].getName());
moveFile(listFiles[i], childTargetFile);
}
sourceFile.delete();
} else {
try {
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
Files.move(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 复制一个文件或文件夹(递归)
*/
public static void copyFile(File sourceFile, File targetFile) {
if (!exists(sourceFile)) {
return;
}
if (sourceFile.isDirectory()) {
if (!targetFile.exists()) {
targetFile.mkdirs();
}
File[] listFiles = sourceFile.listFiles();
for (int i = 0; i < listFiles.length; i++) {
File childTargetFile = new File(targetFile.getPath() + File.separator + listFiles[i].getName());
copyFile(listFiles[i], childTargetFile);
}
} else {
try {
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
Files.copy(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 从字节输入流中读取所有字节
* @param in
* @return
* @throws IOException
*/
public static byte[] readAllBytes(InputStream in) throws IOException {
return readAllBytes(in, 2048);
}
/**
* 从字节输入流中读取所有字节
* @param in
* @param initialSize 初始化字节数组长度,推荐设置为1024的整数倍
* @return
* @throws IOException
*/
public static byte[] readAllBytes(InputStream in, int initialSize) throws IOException {
try (InputStream _in = in) {
int capacity = initialSize;
byte[] buf = new byte[capacity];
int nread = 0;
int rem = buf.length;
for (int n = _in.read(buf, nread, rem); n > 0; n = _in.read(buf, nread, rem)) {
nread += n;
rem -= n;
assert rem >= 0;
if (rem == 0) {
int newCapacity = capacity << 1;
if (newCapacity < 0) {
if (capacity == Integer.MAX_VALUE)
throw new OutOfMemoryError("Required array size too large");
newCapacity = Integer.MAX_VALUE;
}
rem = newCapacity - capacity;
buf = Arrays.copyOf(buf, newCapacity);
capacity = newCapacity;
}
}
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
} catch (IOException e) {
throw e;
}
}
/**
* 从字节输入流中读取所有字符串(UTF-8)
* @param in
* @return
* @throws IOException
*/
public static String readAllString(InputStream in) throws IOException {
return readAllString(in, Charset.forName("UTF-8"));
}
/**
* 从字节输入流中读取所有字符串(指定编码)
* @param in
* @param code
* @return
* @throws IOException
*/
public static String readAllString(InputStream in, Charset code) throws IOException {
if (code == null) {
code = Charset.forName("UTF-8");
}
try (BufferedInputStream bis = new BufferedInputStream(in);
InputStreamReader isr = new InputStreamReader(bis, code.newDecoder());
BufferedReader reader = new BufferedReader(isr)) {
StringBuffer sb = new StringBuffer();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
sb.append(line);
}
return sb.toString();
}
}
/**
* 从字符流中读取所有字符串
* @param r
* @return
* @throws IOException
*/
public static String readAllString(Reader r) throws IOException {
return readAllString(r, 1024);
}
/**
* 从字符流中读取所有字符串
* @param r
* @param initialSize 初始化字符数组长度,推荐设置为1024的整数倍
* @return
* @throws IOException
*/
public static String readAllString(Reader r, int initialSize) throws IOException {
try (BufferedReader br = new BufferedReader(r)) {
StringBuffer sb = new StringBuffer();
char[] b = new char[initialSize];
for (int i = br.read(b); i > 0; i = br.read(b)) {
sb.append(new String(b, 0, i));
}
return sb.toString();
}
}
/**
* 从字节输入流中读取所有字符串(UTF-8),并按行返回List
* @param in
* @return
* @throws IOException
*/
public static List<String> readAllLines(InputStream in) throws IOException {
return readAllLines(in, "UTF-8");
}
/**
* 从字节输入流中读取所有字符串(指定编码),并按行返回List
* @param in
* @param code
* @return
* @throws IOException
*/
public static List<String> readAllLines(InputStream in, String code) throws IOException {
try (Reader r = code == null ? new InputStreamReader(in) : new InputStreamReader(in, code)) {
return readAllLines(r);
}
}
/**
* 从字符输入流中读取所有字符串,并按行返回List
* @param r
* @return
* @throws IOException
*/
public static List<String> readAllLines(Reader r) throws IOException {
try (BufferedReader br = new BufferedReader(r)) {
List<String> result = new ArrayList<>();
for (String line = br.readLine(); line != null; line = br.readLine()) {
result.add(line);
}
return result;
}
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)