commit
45d9f4b4fc
@ -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必须和变量名一致
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user