feat: 固定使用user.home/.jnotepad作为输出目录

This commit is contained in:
songdragon 2023-08-25 00:36:17 +08:00
parent 017156b0eb
commit 8c533b7a6f
3 changed files with 29 additions and 12 deletions

View File

@ -10,6 +10,7 @@ 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;
@ -28,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();
}
}
@ -53,7 +57,7 @@ public abstract class LoadJnotepadConfig {
// 动态添加快捷键
menuItem.setAccelerator(KeyCombination.keyCombination(shortKeyValue));
}
String jsonConfigPath= Paths.get(new Config().getAppConfigDir(), CONFIG_NAME).toString();
String jsonConfigPath = getConfigPath();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(jsonConfigPath))) {
writer.write(JNOTEPAD_CONFIG);
} catch (IOException e) {
@ -61,6 +65,10 @@ public abstract class LoadJnotepadConfig {
}
}
private static String getConfigPath() {
return Paths.get(new Config().getAppConfigDir(), CONFIG_NAME).toString();
}
/**
* 获取快捷键集合
*

View File

@ -5,6 +5,8 @@ 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;
@ -26,8 +28,9 @@ public class Config {
public Config() {
appConfigDir = System.getProperty("user.home") + File.separator + ".jnotepad";
boolean isConfigDirReady = Paths.get(appConfigDir).toFile().mkdirs();
if (!isConfigDirReady) {
Path path = Paths.get(appConfigDir);
boolean isConfigDirReady = path.toFile().mkdirs();
if (!isConfigDirReady && !Files.exists(path)) {
appConfigDir = "/tmp";
}
}
@ -42,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;
}

View File

@ -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"/>-->
<!-- 日志的显式格式 -->