界面主要技术 javafx
自行导出exe文件吧
上代码--------------》
package com;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.File;
import java.util.Objects;
public class EditorFileName extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
// 容器添加子组件
// 指定一个布局类或者根结点
Scene scene = new Scene(group);
primaryStage.setScene(scene);
primaryStage.setTitle("批量修改文件名称");
primaryStage.setHeight(300);
primaryStage.setWidth(400);
//
TextField text = new TextField();
text.setFont(Font.font(16));
text.setLayoutX(110);
text.setLayoutY(30);
// 设置文本框背景提示,需要去除焦点,并去除文本框文字
text.setPromptText("输入替换字符");
text.setFocusTraversable(false);
TextField text2 = new TextField();
text2.setFont(Font.font(16));
text2.setLayoutX(110);
text2.setLayoutY(100);
text2.setPromptText("输入目标字符");
text2.setFocusTraversable(false);
// btn
Button button = new Button("选择文件夹");
//单击事件
button.setOnAction((e) -> {
DirectoryChooser directoryChooser = new DirectoryChooser();
File file = directoryChooser.showDialog(primaryStage);
if (file != null) {
String path = file.getPath();//选择的文件夹路径
String tv1 = text.getText().trim();
String tv2 = text2.getText().trim();
gaiFileName(path, tv1, tv2);
}
});
button.setFont(Font.font(16));
button.setLayoutX(111);
button.setLayoutY(165);
button.setPrefSize(215, 38);
Label label1 = new Label("替换字符");
label1.setFont(Font.font(20));
label1.setLayoutX(20);
label1.setLayoutY(33);
Label label2 = new Label("目标字符");
label2.setFont(Font.font(18));
label2.setLayoutX(20);
label2.setLayoutY(103);
group.getChildren().addAll(text, text2, button, label1, label2);
primaryStage.setResizable(false);
primaryStage.show();
}
/**
* 修改文件名称
*
* @param dir
* @param old
* @param nstr
*/
private void gaiFileName(String dir, String old, String nstr) {
File file = new File(dir);
File[] files = file.listFiles();
if (Objects.nonNull(files) && files.length > 0) {
for (File file1 : files) {
if (file1.isDirectory()) {
gaiFileName(file1.getAbsolutePath(), old, nstr);
} else {
String fileName = file1.getName();
File parentPath = file1.getParentFile();
if (fileName.contains(old)) {
String replace = fileName.replace(old, nstr);
File newDir = new File(parentPath + "/" + replace);
file1.renameTo(newDir);
}
}
}
}
}
}