!13 fix: 读取用户自定义配置文件

Merge pull request !13 from 一个大转盘/master
This commit is contained in:
Luke 2023-08-19 17:49:26 +00:00 committed by Gitee
commit 45d9f4b4fc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -9,7 +9,8 @@ import org.jcnc.jnotepad.Interface.ShortcutKeyInterface;
import org.jcnc.jnotepad.tool.FileUtil;
import org.jcnc.jnotepad.view.manager.ViewManager;
import java.io.File;
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
@ -20,14 +21,36 @@ import java.util.Objects;
public class ShortcutKey implements ShortcutKeyInterface {
@Override
public void createShortcutKeyByConfig() {
String json = "src/main/resources/config/shortcutKey.json";
File jsonFile = new File(json);
// 读取json文件
String jsonData = FileUtil.getJsonStr(jsonFile);
String rootPath =System.getProperty("user.dir");
// 构建JSON文件路径
String jsonFilePath = rootPath +"/config/shortcutKey.json";
InputStream inputStream = getClass().getResourceAsStream("/config/shortcutKey.json");
StringBuffer jsonData = new StringBuffer();
File file = new File(jsonFilePath);
if(file.exists()){
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}else {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = reader.readLine()) != null) {
jsonData.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 转json对象
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Map<String, String> shortcutKeyConfig = gson.fromJson(jsonData, new TypeToken<Map<String, String>>() {
Map<String, String> shortcutKeyConfig = gson.fromJson(jsonData.toString(), new TypeToken<Map<String, String>>() {
}.getType());
for (Map.Entry<String, String> stringObjectEntry : shortcutKeyConfig.entrySet()) {
// 保证json的key必须和变量名一致