2023年4月27日17:44:23

master
zhangmeng 2023-04-27 17:44:38 +08:00
parent aa732af9a4
commit 92ebe9506e
7 changed files with 242 additions and 5 deletions

View File

@ -0,0 +1,150 @@
package com.zhangmeng.tools.controller;
import com.zhangmeng.tools.utils.AlertUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author :
* @version : 1.0
* @date : 2023-04-27 10:24
*/
@Slf4j
public class BatchUpdateFileNameController {
@FXML
public TextField dir;
@FXML
public TextArea result_show;
@FXML
public Button replace_button;
@FXML
public TextField replace_str;
@FXML
public TextField replace_after;
@FXML
public void initialize() {
dir.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
getFileInfos(newValue);
}
});
replace_button.setOnAction(event -> {
if (dir.getText().length() == 0) {
AlertUtils.alert_warning("请选择替换的文件夹");
return;
}
if (replace_str.getText().length() == 0) {
AlertUtils.alert_warning("请输入替换的文字");
return;
}
if (replace_after.getText().length() == 0) {
replace_after.setText("");
}
getFileInfos(dir.getText(), replace_str.getText(), replace_after.getText());
});
}
public void getFileInfos(String path_name) {
Path path = Paths.get(path_name);
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(System.lineSeparator());
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
stringBuilder.append(dir);
stringBuilder.append(System.lineSeparator());
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
stringBuilder.append(file);
stringBuilder.append(System.lineSeparator());
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
e.printStackTrace();
}
stringBuilder.append("文件夹数:").append(dirCount);
stringBuilder.append(System.lineSeparator());
stringBuilder.append("文件数:").append(fileCount);
stringBuilder.append(System.lineSeparator());
result_show.setText(stringBuilder.toString());
}
public void getFileInfos(String path_name, String replace, String replace_after) {
Path path = Paths.get(path_name);
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(System.lineSeparator());
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
dir.toString().replace(replace, replace_after);
stringBuilder.append(dir);
stringBuilder.append(System.lineSeparator());
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String name = file.toString().replace(replace,replace_after);
copy_del_file(file,Paths.get(name));
stringBuilder.append(name);
stringBuilder.append(System.lineSeparator());
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
e.printStackTrace();
}
stringBuilder.append("文件夹数:").append(dirCount);
stringBuilder.append(System.lineSeparator());
stringBuilder.append("文件数:").append(fileCount);
stringBuilder.append(System.lineSeparator());
result_show.setText(stringBuilder.toString());
}
public void copy_del_file(Path source,Path target){
try {
if (!Files.exists(target)){
Files.createFile(target);
}
Files.copy(source, target,StandardCopyOption.REPLACE_EXISTING);//如果文件存在则覆盖
Files.delete(source);//拷贝完删除源文件
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -542,4 +542,8 @@ public class HomeController implements Serializable {
} }
jks_file(flag); jks_file(flag);
} }
public void batch_update_file_name_menu_item(ActionEvent event) {
load_small_tools(14);
}
} }

View File

@ -81,6 +81,7 @@ public class SmallToolsController {
private AnchorPane word_ocr; private AnchorPane word_ocr;
private AnchorPane bar_code; private AnchorPane bar_code;
private AnchorPane pdf_ocr; private AnchorPane pdf_ocr;
private AnchorPane batch_update_file_name;
@FXML @FXML
private ListView<ResourcesUtils.SmallTools> listView; private ListView<ResourcesUtils.SmallTools> listView;
@ -416,6 +417,7 @@ public class SmallToolsController {
case Word_ocr -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); case Word_ocr -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case Bar_Code -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); case Bar_Code -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case Pdf_Ocr -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE)); case Pdf_Ocr -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case BatchUpdateFileName -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
}; };
} }
@ -686,6 +688,16 @@ public class SmallToolsController {
common_method(); common_method();
} }
@FXML
public void batch_update_file_name_menu_item(){
boolean flag = false;
if (batch_update_file_name != null){
flag = true;
}
batch_update_file_name(flag);
}
public void pdf_ocr(boolean flag) { public void pdf_ocr(boolean flag) {
//默认选择第一个 //默认选择第一个
listView.getSelectionModel().select(13); listView.getSelectionModel().select(13);
@ -703,6 +715,25 @@ public class SmallToolsController {
common_method(); common_method();
} }
//batch_update_file_name
@FXML
public void batch_update_file_name(boolean flag) {
//默认选择第一个
listView.getSelectionModel().select(14);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("batch_update_file_name"));
} catch (IOException e) {
e.printStackTrace();
}
batch_update_file_name = root;
} else {
root = batch_update_file_name;
}
common_method();
}
private void common_method() { private void common_method() {
splitPane.getItems().remove(1); splitPane.getItems().remove(1);
@ -820,11 +851,11 @@ public class SmallToolsController {
ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView"); ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
listView.getSelectionModel().select(index); listView.getSelectionModel().select(index);
} }
@FXML
public void socket_server_menu_item(ActionEvent event) { public void socket_server_menu_item(ActionEvent event) {
load_server_tools(3); load_server_tools(3);
} }
@FXML
public void telephone_menu_item(ActionEvent event) { public void telephone_menu_item(ActionEvent event) {
boolean flag = false; boolean flag = false;
if (telephone != null){ if (telephone != null){
@ -832,12 +863,12 @@ public class SmallToolsController {
} }
telephone(flag); telephone(flag);
} }
@FXML
public void mybatis_plus_gen_menu_item(ActionEvent event) { public void mybatis_plus_gen_menu_item(ActionEvent event) {
load_mysql_tools(1); load_mysql_tools(1);
} }
@FXML
public void JsonView_menu_item(ActionEvent event) { public void JsonView_menu_item(ActionEvent event) {
boolean flag =false; boolean flag =false;
if (json_view != null){ if (json_view != null){
@ -845,7 +876,7 @@ public class SmallToolsController {
} }
json_view(flag); json_view(flag);
} }
@FXML
public void maven_jar_install_menu_item(ActionEvent event) { public void maven_jar_install_menu_item(ActionEvent event) {
boolean flag =false; boolean flag =false;
if (maven_install_jar != null){ if (maven_install_jar != null){
@ -854,5 +885,29 @@ public class SmallToolsController {
maven_install_jar(flag); maven_install_jar(flag);
} }
@FXML
public void word_ocr_menu_item(ActionEvent event) {
boolean flag =false;
if (word_ocr != null){
flag = true;
}
word_ocr(flag);
}
@FXML
public void bar_code_menu_item(ActionEvent event) {
boolean flag =false;
if (bar_code != null){
flag = true;
}
bar_code(flag);
}
@FXML
public void pdf_menu_item(ActionEvent event) {
boolean flag =false;
if (pdf_ocr != null){
flag = true;
}
pdf_ocr(flag);
}
} }

View File

@ -128,6 +128,7 @@ public class ResourcesUtils {
Word_ocr("文字识别ocr",11), Word_ocr("文字识别ocr",11),
Bar_Code("条形码识别",12), Bar_Code("条形码识别",12),
Pdf_Ocr("pdf识别",13), Pdf_Ocr("pdf识别",13),
BatchUpdateFileName("批量修改文件名",14),
; ;
SmallTools(String title, int index) { SmallTools(String title, int index) {

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.BatchUpdateFileNameController">
<children>
<Label layoutX="134.0" layoutY="117.0" text="批量修改的文件夹:" />
<TextField fx:id="dir" layoutX="252.0" layoutY="113.0" prefHeight="25.0" prefWidth="293.0" />
<TextArea fx:id="result_show" layoutX="252.0" layoutY="190.0" prefHeight="311.0" prefWidth="820.0" AnchorPane.bottomAnchor="148.0" AnchorPane.leftAnchor="252.0" AnchorPane.rightAnchor="128.0" AnchorPane.topAnchor="190.0" />
<Label layoutX="168.0" layoutY="190.0" text="处理结果:" />
<Button fx:id="replace_button" layoutX="1017.0" layoutY="113.0" mnemonicParsing="false" text="替换" />
<Label layoutX="569.0" layoutY="117.0" text="替换的字符:" />
<TextField fx:id="replace_str" layoutX="645.0" layoutY="113.0" prefHeight="25.0" prefWidth="106.0" />
<TextField fx:id="replace_after" layoutX="870.0" layoutY="113.0" prefHeight="25.0" prefWidth="113.0" />
<Label layoutX="779.0" layoutY="117.0" text="替换后的字符:" />
</children>
</AnchorPane>

View File

@ -46,6 +46,7 @@
<MenuItem mnemonicParsing="false" text="word-ocr" onAction="#word_ocr_menu_item"/> <MenuItem mnemonicParsing="false" text="word-ocr" onAction="#word_ocr_menu_item"/>
<MenuItem mnemonicParsing="false" text="条形码识别" onAction="#bar_code_menu_item"/> <MenuItem mnemonicParsing="false" text="条形码识别" onAction="#bar_code_menu_item"/>
<MenuItem mnemonicParsing="false" text="pdf识别" onAction="#pdf_menu_item"/> <MenuItem mnemonicParsing="false" text="pdf识别" onAction="#pdf_menu_item"/>
<MenuItem mnemonicParsing="false" text="批量修改文件名" onAction="#batch_update_file_name_menu_item"/>
</items> </items>
</Menu> </Menu>

View File

@ -39,6 +39,10 @@
<MenuItem mnemonicParsing="false" text="手机号工具" onAction="#telephone_menu_item"/> <MenuItem mnemonicParsing="false" text="手机号工具" onAction="#telephone_menu_item"/>
<MenuItem mnemonicParsing="false" text="JsonView" onAction="#JsonView_menu_item"/> <MenuItem mnemonicParsing="false" text="JsonView" onAction="#JsonView_menu_item"/>
<MenuItem mnemonicParsing="false" text="maven-jar-install" onAction="#maven_jar_install_menu_item"/> <MenuItem mnemonicParsing="false" text="maven-jar-install" onAction="#maven_jar_install_menu_item"/>
<MenuItem mnemonicParsing="false" text="word-ocr" onAction="#word_ocr_menu_item"/>
<MenuItem mnemonicParsing="false" text="条形码识别" onAction="#bar_code_menu_item"/>
<MenuItem mnemonicParsing="false" text="pdf识别" onAction="#pdf_menu_item"/>
<MenuItem mnemonicParsing="false" text="批量修改文件名" onAction="#batch_update_file_name_menu_item"/>
</items> </items>
</Menu> </Menu>