diff --git a/src/main/java/xyz/wbsite/jmacro/JProp.java b/src/main/java/xyz/wbsite/jmacro/JProp.java new file mode 100644 index 0000000..94d9b1e --- /dev/null +++ b/src/main/java/xyz/wbsite/jmacro/JProp.java @@ -0,0 +1,99 @@ +package xyz.wbsite.jmacro; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.json.JSONUtil; +import cn.hutool.setting.dialect.Props; + +import java.io.File; +import java.nio.charset.StandardCharsets; + +/** + * 首选项配置 + */ +public class JProp { + + private static final JProp instance = new JProp(); + + private final File propFile; + + private final Props props; + + public static JProp getInstance() { + return instance; + } + + private JProp() { + this.propFile = new File("prop.ini"); + if (!propFile.exists()) { + FileUtil.touch(propFile.getAbsolutePath()); + this.props = new Props(propFile, StandardCharsets.UTF_8); + // 做一些初始化操作 + } else { + this.props = new Props(propFile, StandardCharsets.UTF_8); + } + } + + public static void main(String[] args) { + String[] keywords = JProp.getInstance().getStringArray("keywords", new String[]{}); + + System.out.println(JSONUtil.toJsonStr(keywords)); + } + + private void save() { + this.props.store(propFile.getAbsolutePath()); + } + + public String getString(String key, String defaultValue) { + return this.props.getStr(key, defaultValue); + } + + public String[] getStringArray(String key, String[] defaultValue) { + return this.props.getStr(key, "").split(","); + } + + public int getInt(String key, int defaultValue) { + return this.props.getInt(key, defaultValue); + } + + public long getLong(String key, long defaultValue) { + return this.props.getLong(key, defaultValue); + } + + public float getFloat(String key, float defaultValue) { + return this.props.getFloat(key, defaultValue); + } + + public double getDouble(String key, double defaultValue) { + return this.props.getDouble(key, defaultValue); + } + + public void setString(String key, String value) { + this.props.setProperty(key, value); + this.save(); + } + + public void setStringArray(String key, String[] value) { + this.props.setProperty(key, String.join(",", value)); + this.save(); + } + + public void setInt(String key, int value) { + this.props.setProperty(key, String.valueOf(value)); + this.save(); + } + + public void setLong(String key, long value) { + this.props.setProperty(key, String.valueOf(value)); + this.save(); + } + + public void setFloat(String key, float value) { + this.props.setProperty(key, String.valueOf(value)); + this.save(); + } + + public void setDouble(String key, double value) { + this.props.setProperty(key, String.valueOf(value)); + this.save(); + } +}