!31 feat: #I7VS0O 统一配置文件默认输出目录
Merge pull request !31 from songdragon/feature-I7VS0O
This commit is contained in:
commit
240abc1e91
@ -3,12 +3,15 @@ package org.jcnc.jnotepad.app.config;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.input.KeyCombination;
|
||||
import org.jcnc.jnotepad.app.entity.ShortcutKey;
|
||||
import org.jcnc.jnotepad.init.Config;
|
||||
import org.jcnc.jnotepad.tool.LogUtil;
|
||||
import org.jcnc.jnotepad.tool.PopUpUtil;
|
||||
import org.jcnc.jnotepad.ui.menu.JNotepadMenuBar;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -26,15 +29,18 @@ public abstract class LoadJnotepadConfig {
|
||||
Logger logger = LogUtil.getLogger(this.getClass());
|
||||
|
||||
public final void load() {
|
||||
String path = getConfigPath();
|
||||
if (!Files.exists(Paths.get(path))) {
|
||||
// 不存在则创建
|
||||
createConfig();
|
||||
}
|
||||
// 判断是否存在这个配置文件
|
||||
try (InputStream inputStream = new FileInputStream(CONFIG_NAME)) {
|
||||
try (InputStream inputStream = new FileInputStream(getConfigPath())) {
|
||||
logger.info("正在加载配置文件...");
|
||||
// 存在则加载
|
||||
loadConfig(inputStream);
|
||||
} catch (IOException e) {
|
||||
logger.info("未检测到配置文件!");
|
||||
// 不存在则创建
|
||||
createConfig();
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,13 +57,18 @@ public abstract class LoadJnotepadConfig {
|
||||
// 动态添加快捷键
|
||||
menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue));
|
||||
}
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CONFIG_NAME))) {
|
||||
String jsonConfigPath = getConfigPath();
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(jsonConfigPath))) {
|
||||
writer.write(JNOTEPAD_CONFIG);
|
||||
} catch (IOException e) {
|
||||
PopUpUtil.errorAlert("错误", "读写错误", "配置文件读写错误!");
|
||||
}
|
||||
}
|
||||
|
||||
private static String getConfigPath() {
|
||||
return Paths.get(new Config().getAppConfigDir(), CONFIG_NAME).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取快捷键集合
|
||||
*
|
||||
|
||||
@ -5,6 +5,9 @@ import org.slf4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.jcnc.jnotepad.constants.AppConstants.CH_LANGUAGE_PACK_NAME;
|
||||
@ -20,8 +23,18 @@ public class Config {
|
||||
|
||||
private String languagePackName;
|
||||
|
||||
private String appConfigDir;
|
||||
Logger logger = LogUtil.getLogger(this.getClass());
|
||||
|
||||
public Config() {
|
||||
appConfigDir = System.getProperty("user.home") + File.separator + ".jnotepad";
|
||||
Path path = Paths.get(appConfigDir);
|
||||
boolean isConfigDirReady = path.toFile().mkdirs();
|
||||
if (!isConfigDirReady && !Files.exists(path)) {
|
||||
appConfigDir = "/tmp";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件中读取属性配置。
|
||||
*
|
||||
@ -32,13 +45,19 @@ public class Config {
|
||||
|
||||
//设置语言包
|
||||
languagePackName = EN_LANGUAGE_PACK_NAME;
|
||||
try (InputStream inputStream = new FileInputStream(languagePackName)) {
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
|
||||
properties.load(reader);
|
||||
} catch (IOException e) {
|
||||
String languageFilePath = Paths.get(appConfigDir, languagePackName).toString();
|
||||
|
||||
if (!Files.exists(Paths.get(languageFilePath))) {
|
||||
// 如果读取出错,则调用初始化方法
|
||||
initializePropertiesFile();
|
||||
}
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(languageFilePath)) {
|
||||
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
|
||||
properties.load(reader);
|
||||
} catch (IOException ignored) {
|
||||
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
@ -51,7 +70,8 @@ public class Config {
|
||||
|
||||
Properties enLanguagePack = getEnglishLanguagePack();
|
||||
|
||||
try (OutputStream outputStream = new FileOutputStream(CH_LANGUAGE_PACK_NAME)) {
|
||||
String chineseFilePath = Paths.get(appConfigDir, CH_LANGUAGE_PACK_NAME).toString();
|
||||
try (OutputStream outputStream = new FileOutputStream(chineseFilePath)) {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
|
||||
chLanguagePack.store(writer, JNOTEPAD_CH_LANGUAGE_PACK_NAME);
|
||||
|
||||
@ -59,7 +79,8 @@ public class Config {
|
||||
logger.info("未检测到中文语言包!");
|
||||
}
|
||||
|
||||
try (OutputStream outputStream = new FileOutputStream(EN_LANGUAGE_PACK_NAME)) {
|
||||
String enFilePath = Paths.get(appConfigDir, EN_LANGUAGE_PACK_NAME).toString();
|
||||
try (OutputStream outputStream = new FileOutputStream(enFilePath)) {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); // 使用 UTF-8 编码
|
||||
enLanguagePack.store(writer, JNOTEPAD_EN_LANGUAGE_PACK_NAME);
|
||||
|
||||
@ -114,4 +135,7 @@ public class Config {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public String getAppConfigDir() {
|
||||
return appConfigDir;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,13 +3,13 @@
|
||||
<!-- scanPeriod属性设置监测配置文件修改的时间间隔,默认单位为毫秒,在scan为true时才生效 -->
|
||||
<!-- debug属性如果为true时,会打印出logback内部的日志信息 -->
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 定义参数常量 -->JJ
|
||||
<!-- 定义参数常量 -->
|
||||
<!-- 日志级别:TRACE<DEBUG<INFO<WARN<ERROR,其中常用的是DEBUG、INFO和ERROR -->
|
||||
<property name="log.level" value="debug"/>
|
||||
<!-- 文件保留时间 -->
|
||||
<property name="log.maxHistory" value="30"/>
|
||||
<!-- 日志存储路径 -->
|
||||
<property name="log.filePath" value="logs"/>
|
||||
<property name="log.filePath" value="${user.home}/.jnotepad/logs"/>
|
||||
<!--打包时,请使用下方的路径-->
|
||||
<!--<property name="log.filePath" value="../logs"/>-->
|
||||
<!-- 日志的显式格式 -->
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user