Skip to content

Commit b9d7c20

Browse files
committed
feat: Initial commit with basic software functionality
- Encoding conversion between "GBK" and "UTF-8" - Find files by path - Support filtering files based on file extensions
1 parent 579e3d7 commit b9d7c20

File tree

14 files changed

+560
-0
lines changed

14 files changed

+560
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
/.idea
3+
.DS_Store
4+
/target

pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.zzhow</groupId>
8+
<artifactId>MagicEncoding</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<name>MagicEncoding</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<junit.version>5.10.0</junit.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.openjfx</groupId>
20+
<artifactId>javafx-controls</artifactId>
21+
<version>21</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.openjfx</groupId>
25+
<artifactId>javafx-fxml</artifactId>
26+
<version>21</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<version>${junit.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<version>${junit.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>3.11.0</version>
49+
<configuration>
50+
<source>21</source>
51+
<target>21</target>
52+
</configuration>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.openjfx</groupId>
56+
<artifactId>javafx-maven-plugin</artifactId>
57+
<version>0.0.8</version>
58+
<executions>
59+
<execution>
60+
<!-- Default configuration for running with: mvn clean javafx:run -->
61+
<id>default-cli</id>
62+
<configuration>
63+
<mainClass>com.zzhow.magicencoding/com.zzhow.magicencoding.ui.Application</mainClass>
64+
<launcher>app</launcher>
65+
<jlinkZipName>app</jlinkZipName>
66+
<jlinkImageName>app</jlinkImageName>
67+
<noManPages>true</noManPages>
68+
<stripDebug>true</stripDebug>
69+
<noHeaderFiles>true</noHeaderFiles>
70+
</configuration>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.zzhow.magicencoding;
2+
3+
import com.zzhow.magicencoding.ui.Application;
4+
5+
/**
6+
* @author ZZHow
7+
* @date 2024/9/3
8+
*/
9+
public class MainClass {
10+
public static void main(String[] args) {
11+
Application.main(args);
12+
}
13+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.zzhow.magicencoding.controller;
2+
3+
import com.zzhow.magicencoding.service.FileService;
4+
import com.zzhow.magicencoding.service.impl.FileServiceImpl;
5+
import com.zzhow.magicencoding.ui.About;
6+
import com.zzhow.magicencoding.utils.MessageBox;
7+
import javafx.fxml.FXML;
8+
import javafx.scene.control.ChoiceBox;
9+
import javafx.scene.control.ListView;
10+
import javafx.scene.control.TextField;
11+
12+
public class MainController {
13+
14+
// 文件服务类
15+
private final FileService fileService = FileServiceImpl.getInstance();
16+
17+
@FXML
18+
private ChoiceBox<String> originChoiceBox;
19+
@FXML
20+
private ChoiceBox<String> targetChoiceBox;
21+
@FXML
22+
private TextField pathTextField;
23+
@FXML
24+
private TextField endWithTextField;
25+
@FXML
26+
private ListView<String> filesListView;
27+
28+
public void initialize() {
29+
originChoiceBox.getItems().addAll("GBK", "UTF-8");
30+
originChoiceBox.setValue("GBK");
31+
targetChoiceBox.getItems().addAll("UTF-8", "GBK");
32+
targetChoiceBox.setValue("UTF-8");
33+
}
34+
35+
public void clearFilesPath() {
36+
fileService.clearTargetFileList();
37+
filesListView.setItems(null);
38+
}
39+
40+
@FXML
41+
private void onReset() {
42+
this.clearFilesPath();
43+
pathTextField.setText("");
44+
endWithTextField.setText("");
45+
}
46+
47+
@FXML
48+
private void onFindFiles() {
49+
this.clearFilesPath();
50+
String absolutePath = pathTextField.getText();
51+
String endWith = endWithTextField.getText();
52+
53+
filesListView.setItems(fileService.findFiles(absolutePath, endWith));
54+
}
55+
56+
@FXML
57+
private void onTransform() {
58+
String absolutePath = pathTextField.getText();
59+
String originCharset = originChoiceBox.getValue();
60+
String targetCharset = targetChoiceBox.getValue();
61+
62+
if (fileService.transform(absolutePath, originCharset, targetCharset)) {
63+
MessageBox.success("执行成功", "已将" + fileService.getTargetFileList().size()
64+
+ "个文件从 \"" + originCharset + "\" 转为 \"" + targetCharset + "\"");
65+
} else {
66+
MessageBox.error("执行失败", "请重试");
67+
}
68+
}
69+
70+
@FXML
71+
private void onAbout() {
72+
About.open();
73+
}
74+
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.zzhow.magicencoding.service;
2+
3+
import javafx.collections.ObservableList;
4+
5+
import java.util.ArrayList;
6+
7+
/**
8+
* @author ZZHow
9+
* @date 2024/9/3
10+
*/
11+
public interface FileService {
12+
13+
/**
14+
* 获得命中文件绝对路径的集合
15+
*
16+
* @return 命中文件绝对路径的集合
17+
*/
18+
ArrayList<String> getTargetFileList();
19+
20+
/**
21+
* 清空命中文件绝对路径的集合
22+
*/
23+
void clearTargetFileList();
24+
25+
/**
26+
* 查找文件
27+
*
28+
* @param absolutePath 需要查找的文件夹绝对路径
29+
* @param endWith 后缀名
30+
* @return 命中文件绝对路径的集合
31+
*/
32+
ObservableList<String> findFiles(String absolutePath, String endWith);
33+
34+
/**
35+
* 转换编码
36+
*
37+
* @param absolutePath 需要查找的文件夹绝对路径
38+
* @param originCharset 原始字符集
39+
* @param targetCharset 目标字符集
40+
* @return true - 转换成功,false - 转换失败
41+
*/
42+
boolean transform(String absolutePath, String originCharset, String targetCharset);
43+
44+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.zzhow.magicencoding.service.impl;
2+
3+
import com.zzhow.magicencoding.service.FileService;
4+
import com.zzhow.magicencoding.utils.MessageBox;
5+
import com.zzhow.magicencoding.utils.MyFiles;
6+
import javafx.collections.FXCollections;
7+
import javafx.collections.ObservableList;
8+
9+
import java.io.File;
10+
import java.util.ArrayList;
11+
12+
/**
13+
* @author ZZHow
14+
* @date 2024/9/3
15+
*/
16+
public class FileServiceImpl implements FileService {
17+
private final ArrayList<String> targetFileList = new ArrayList<>();
18+
private static final FileService fileService = new FileServiceImpl();
19+
20+
private FileServiceImpl() {
21+
}
22+
23+
public static FileService getInstance() {
24+
return fileService;
25+
}
26+
27+
public ArrayList<String> getTargetFileList() {
28+
return this.targetFileList;
29+
}
30+
31+
public void clearTargetFileList() {
32+
this.targetFileList.clear();
33+
}
34+
35+
@Override
36+
public ObservableList<String> findFiles(String absolutePath, String endWith) {
37+
File currentFolder = new File(absolutePath);
38+
File[] files = currentFolder.listFiles();
39+
40+
if (files == null || files.length == 0) {
41+
MessageBox.error("当前文件夹下没有满足条件的文件", "请检查设置的条件");
42+
43+
return null;
44+
}
45+
46+
MyFiles.find(absolutePath, endWith, targetFileList);
47+
48+
// 打印满足条件的文件的绝对路径
49+
for (String string : targetFileList) {
50+
System.out.println(string);
51+
}
52+
53+
return FXCollections.observableList(targetFileList);
54+
}
55+
56+
@Override
57+
public boolean transform(String absolutePath, String originCharset, String targetCharset) {
58+
if (targetFileList.isEmpty()) {
59+
MessageBox.error("当前没有命中的文件", "请先查找文件");
60+
61+
return false;
62+
}
63+
64+
// 开始转换编码
65+
String outputPath = absolutePath + "/MagicEncodingOutput";
66+
File outputFolder = new File(outputPath);
67+
if (outputFolder.exists()) {
68+
MyFiles.deleteFolder(outputPath);
69+
outputFolder.delete();
70+
}
71+
72+
if (!outputFolder.mkdir()) {
73+
MessageBox.error("创建输出文件夹失败", "请重试");
74+
75+
return false;
76+
}
77+
78+
for (String originPath : targetFileList) {
79+
String targetPath = outputPath + originPath.split(absolutePath)[1];
80+
MyFiles.transform(originPath, targetPath, originCharset, targetCharset);
81+
}
82+
83+
return true;
84+
}
85+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.zzhow.magicencoding.ui;
2+
3+
import com.zzhow.magicencoding.MainClass;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Scene;
6+
import javafx.scene.image.Image;
7+
import javafx.scene.layout.Pane;
8+
import javafx.stage.Stage;
9+
10+
import java.io.IOException;
11+
import java.util.Objects;
12+
13+
/**
14+
* @author ZZHow
15+
* @date 2024/9/3
16+
*/
17+
public class About {
18+
public static void open() {
19+
Stage stage = new Stage();
20+
stage.setTitle("MagicEncoding 1.0.0 - About");
21+
Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png")));
22+
stage.getIcons().add(icon);
23+
stage.setResizable(false);
24+
try {
25+
Pane load = FXMLLoader.load(Objects.requireNonNull(About.class.getResource("about-view.fxml")));
26+
Scene scene = new Scene(load);
27+
stage.setScene(scene);
28+
stage.show();
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.zzhow.magicencoding.ui;
2+
3+
import com.zzhow.magicencoding.MainClass;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Scene;
6+
import javafx.scene.image.Image;
7+
import javafx.stage.Stage;
8+
9+
import java.io.IOException;
10+
import java.util.Objects;
11+
12+
public class Application extends javafx.application.Application {
13+
@Override
14+
public void start(Stage stage) throws IOException {
15+
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("main-view.fxml"));
16+
Scene scene = new Scene(fxmlLoader.load(), 640, 480);
17+
stage.setTitle("MagicEncoding");
18+
stage.setScene(scene);
19+
Image icon = new Image(Objects.requireNonNull(MainClass.class.getResourceAsStream("/image/icon.png")));
20+
stage.getIcons().add(icon);
21+
stage.setResizable(false);
22+
stage.show();
23+
}
24+
25+
public static void main(String[] args) {
26+
launch();
27+
}
28+
}

0 commit comments

Comments
 (0)