目的: jar パッケージのアップグレードでは以前の jar パッケージを置き換える必要があり、バージョンのサフィックスが一貫していないため、手動での比較が必要です。
機能: コードがアップグレードされた後、同じモジュールの jar パッケージは同じですが、サフィックスが異なります。名前を比較して置換するための小さなツールを作成し、手動チェックを回避します
技術的なポイント: Java Swing
利点: コードを作成した後、パッケージをダブルクリックして直接実行すると、フロントエンド ページを視覚化するのに便利で、コードが少なくなり、使いやすくなります。
Java Swing の基本コンポーネントの紹介:
JPanel(パネル)
JPanel は Java Swingパッケージの一部であり、コンポーネントのセットを保存できるコンテナです。JPanel の主なタスクはコンポーネントを整理することです。JPanel ではコンポーネントをより適切に整理するためにさまざまなレイアウトを設定できますが、JPanel にはタイトル バーがありません。
//创建一个新的 JPanel双缓冲区和流布局。
JPanel jPanel = new JPanel()
//创建一个新的 JPanel与 FlowLayout和指定的缓冲策略。
JPanel jPanel = new JPanel(boolean isDoubleBuffered)
//使用指定的布局管理器创建一个新的缓冲JPanel
JPanel jPanel = new JPanel(LayoutManager layout)
//使用指定的布局管理器和缓冲策略创建一个新的JPanel。
JPanel jPanel = new JPanel(LayoutManager layout, boolean isDoubleBuffered)
JLabel ラベル
JLabel は、テキスト、画像、またはその両方を含めることができる JPanel 内のラベルです。
JLabel jLabel = new JLabel("Hi,我是标签");
//添加到面板
Jpanel1.add(jLabel);
//将面板添加到窗口
jFrame.add(Jpanel1);
ウィジェットの実装原則
参照ボックスに従って置換したいディレクトリを選択し、選択したディレクトリに従ってバックグラウンドでディレクトリ内のすべての jar ファイルを読み取り、誤った置換操作を防ぐためのプロンプトを表示します。同意をクリックした後、元の jar パッケージが配置されているディレクトリと新しい jar パッケージが配置されているディレクトリを比較して置換します。同時に、ログがページ コンソールに出力され、ログ ファイルをローカル ディレクトリに保存することもできます。
public class MainFrame {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame("jar包批量模糊替换");
// Setting the width and height of frame
frame.setSize(500, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
JPanel panel = new JPanel();
panel.setBounds(10,10,500,220);
// // 添加面板
frame.add(panel);
JTextArea infoText4 = new JTextArea();
/*
* 调用用户定义的方法并添加组件到面板
*/
placeComponents(panel,infoText4);
// 设置界面可见
frame.setVisible(true);
frame.setBounds(500, 500, 650, 320);
}
private static void placeComponents(JPanel panel,JTextArea infoText4) {
/* 布局部分我们这边不多做介绍
* 这边设置布局为 null
*/
panel.setLayout(null);
// 创建 JLabel
JLabel userLabel = new JLabel("执行更名目录");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);
/*
* 创建文本域用于用户输入
*/
final JTextField infoText = new JTextField();
// 第一行输入框
infoText.setBounds(150, 20, 350, 25);
panel.add(infoText);
JLabel userLabel2 = new JLabel("目标参考目录");
userLabel2.setBounds(10, 50, 130, 25);
panel.add(userLabel2);
final JTextField infoText2 = new JTextField();
// 第er行输入框
infoText2.setBounds(150, 50, 350, 25);
panel.add(infoText2);
// 输入密码的文本域
JLabel passwordLabel2 = new JLabel("模糊匹配规则");
passwordLabel2.setBounds(10, 80, 130, 25);
panel.add(passwordLabel2);
// 第3行输入框
final JTextField infoText3 = new JTextField();
infoText3.setBounds(150, 80, 350, 25);
infoText3.setText("bemp");
infoText3.setEditable(false);
panel.add(infoText3);
// 按钮
JButton loginButton = new JButton("浏览");
loginButton.setBounds(500, 20, 100, 25);
loginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
infoText.setText(jButton1ActionPerformed(evt));
}
});
panel.add(loginButton);
JButton loginButton1 = new JButton("浏览");
loginButton1.setBounds(500, 50, 100, 25);
loginButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
infoText2.setText(jButton1ActionPerformed(evt));
}
});
panel.add(loginButton1);
//执行
JButton loginButton2 = new JButton("执行");
loginButton2.setBounds(150, 110, 350, 25);
JScrollPane scrollpane=new JScrollPane();//创建滚动条面板
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpane.setBounds(20, 150, 600, 80);
scrollpane.setViewportView(infoText4);
panel.add(scrollpane);
infoText4.setLineWrap(true);
loginButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
String text = infoText.getText();
List<String> fileList = getFileList(text);
if ("".equals(text) && "".equals(infoText2.getText())){
JOptionPane.showConfirmDialog(null, "请先选择目录","提示", JOptionPane.OK_CANCEL_OPTION);
}else {
int i = JOptionPane.showConfirmDialog(null, "检测到"+text+"目录下含有"+fileList.size()+"个可替换文件是否需要替换","提示", JOptionPane.OK_CANCEL_OPTION);
if (i == 0){
String text1 = infoText2.getText();
Date date = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Logger.log("模糊替换开始执行"+sdf.format(date),text);
infoText4.append("模糊替换开始执行"+sdf.format(date));
infoText4.append("\n");
Changes(evt,text,text1,infoText4);
Date date1 = new Date();
// Logger.log("模糊替换执行结束"+sdf.format(date1),text);
infoText4.append("模糊替换结束执行"+sdf.format(date1));
infoText4.append("\n");
}
}
}
});
panel.add(loginButton2);
}
private static String jButton1ActionPerformed(ActionEvent evt){
JFileChooser chooser = new JFileChooser(); //文件选择
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(chooser); //打开文件选择窗
File openFile = chooser.getSelectedFile(); //获取选择的文件
String textPath = openFile.getPath(); //获取选择文件的路径
return textPath;
}
private static void Changes(ActionEvent evt,String text,String text1,JTextArea infoText4){
List<String> fileList = getFileList(text);
//参考目录
List<String> fileList1 = getFileList(text1);
//根据自自己规则进行匹配
for (int i = 0; i < fileList.size(); i++) {
String filename = fileList.get(i).substring(text.length());
for (int j = 0; j < fileList1.size(); j++) {
String substring = filename.substring(1, 5);
if (fileList1.get(j).contains(filename) && "bemp".equals(filename.substring(1, 5))){
if (!fileList.get(i).substring(text.length()+1).equals(fileList1.get(j).substring(text1.length()+1))){
//替换名称
File file = new File(fileList.get(i));
//修改成功
boolean b = file.renameTo(new File(text+fileList1.get(j).substring(text1.length())));
// Logger.log(fileList.get(i).substring(text.length()+1)+" -> "+fileList1.get(j).substring(text1.length()+1),text);
infoText4.append(fileList.get(i).substring(text.length()+1)+" -> "+fileList1.get(j).substring(text1.length()+1));
infoText4.append("\n");
}
break;
}
}
}
}
public static List<String> getFileList(String strPath) {
ArrayList<String> filelist = new ArrayList<>();
File dir = new File(strPath.trim());
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory()) { // 判断是文件还是文件夹
//文件夹不管
// getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
} else {
String strFileName = files[i].getAbsolutePath();
if (strFileName.contains(".jar") && strFileName.contains("xxx")){
filelist.add(strFileName);
}
}
}
}
return filelist;
}
}
ファイル生成ログ
public class Logger {
public static void log(String arg,String text){
PrintStream out=null;
try {
if (!new File(text+"/logs").exists()){
new File(text+"/logs").mkdir();
}
//指向一个文件日志
Date nowTime=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String path = text+"/logs/"+"/bemp_jar_ren_log"+sdf.format(nowTime)+".txt";
out=new PrintStream(new FileOutputStream(path,true));
//改变输出方向,输出到out中
System.setOut(out);
System.out.println(arg);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(out !=null){
out.close();
}
}
}
}