add store method

This commit is contained in:
Looly 2020-09-10 18:11:04 +08:00
parent 76c6aeeb56
commit d23bdd9280
6 changed files with 95 additions and 15 deletions

View File

@ -6,7 +6,8 @@
# 5.4.3 (2020-09-10)
### 新特性
* 【core 】 使用静态的of方法来new对象pr#177@Gitee
* 【core 】 使用静态的of方法来new对象pr#177@Gitee
* 【setting】 Setting增加store无参方法issue#1072@Github
### Bug修复

View File

@ -2441,6 +2441,20 @@ public class FileUtil extends PathUtil {
return new PrintWriter(getWriter(file, charset, isAppend));
}
/**
* 获得一个打印写入对象可以有print
*
* @param file 文件
* @param charset 字符集
* @param isAppend 是否追加
* @return 打印对象
* @throws IORuntimeException IO异常
* @since 5.4.3
*/
public static PrintWriter getPrintWriter(File file, Charset charset, boolean isAppend) throws IORuntimeException {
return new PrintWriter(getWriter(file, charset, isAppend));
}
/**
* 获取当前系统的换行分隔符
*

View File

@ -2,6 +2,7 @@ package cn.hutool.setting;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.FileResource;
@ -227,6 +228,18 @@ public class Setting extends AbsSetting implements Map<String, String> {
}
/**
* 获得设定文件的URL
*
* @return 获得设定文件的路径
* @since 5.4.3
*/
public URL getSettingUrl() {
return this.settingUrl;
}
/**
* 获得设定文件的路径
*
* @return 获得设定文件的路径
*/
public String getSettingPath() {
@ -334,6 +347,17 @@ public class Setting extends AbsSetting implements Map<String, String> {
// --------------------------------------------------------------------------------- Functions
/**
* 持久化当前设置会覆盖掉之前的设置<br>
* 持久化不会保留之前的分组注意如果配置文件在jar内部或者在exe中此方法会报错
*
* @since 5.4.3
*/
public void store() {
Assert.notNull(this.settingUrl, "Setting path must be not null !");
store(FileUtil.file(this.settingUrl));
}
/**
* 持久化当前设置会覆盖掉之前的设置<br>
* 持久化不会保留之前的分组
@ -341,10 +365,21 @@ public class Setting extends AbsSetting implements Map<String, String> {
* @param absolutePath 设置文件的绝对路径
*/
public void store(String absolutePath) {
store(FileUtil.touch(absolutePath));
}
/**
* 持久化当前设置会覆盖掉之前的设置<br>
* 持久化不会保留之前的分组
*
* @param file 设置文件
* @since 5.4.3
*/
public void store(File file) {
if (null == this.settingLoader) {
settingLoader = new SettingLoader(this.groupedMap, this.charset, this.isUseVariable);
}
settingLoader.store(absolutePath);
settingLoader.store(file);
}
/**

View File

@ -3,6 +3,7 @@ package cn.hutool.setting;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.UrlResource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
@ -10,6 +11,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
@ -168,9 +170,22 @@ public class SettingLoader {
* @param absolutePath 设置文件的绝对路径
*/
public void store(String absolutePath) {
store(FileUtil.touch(absolutePath));
}
/**
* 持久化当前设置会覆盖掉之前的设置<br>
* 持久化会不会保留之前的分组
*
* @param file 设置文件
* @since 5.4.3
*/
public void store(File file) {
Assert.notNull(file, "File to store must be not null !");
log.debug("Store Setting to [{}]...", file.getAbsolutePath());
PrintWriter writer = null;
try {
writer = FileUtil.getPrintWriter(absolutePath, charset, false);
writer = FileUtil.getPrintWriter(file, charset, false);
store(writer);
} finally {
IoUtil.close(writer);

View File

@ -30,6 +30,7 @@ public class PropsTest {
@Test
public void propTest() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("test.properties");
String user = props.getProperty("user");
Assert.assertEquals(user, "root");
@ -41,6 +42,7 @@ public class PropsTest {
@Test
@Ignore
public void propTestForAbsPAth() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("d:/test.properties");
String user = props.getProperty("user");
Assert.assertEquals(user, "root");

View File

@ -9,50 +9,63 @@ import cn.hutool.setting.Setting;
/**
* Setting单元测试
* @author Looly
*
* @author Looly
*/
public class SettingTest {
@Test
public void settingTest(){
public void settingTest() {
//noinspection MismatchedQueryAndUpdateOfCollection
Setting setting = new Setting("test.setting", true);
String driver = setting.getByGroup("driver", "demo");
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
//本分组变量替换
String user = setting.getByGroup("user", "demo");
Assert.assertEquals("rootcom.mysql.jdbc.Driver", user);
//跨分组变量替换
String user2 = setting.getByGroup("user2", "demo");
Assert.assertEquals("rootcom.mysql.jdbc.Driver", user2);
//默认值测试
String value = setting.getStr("keyNotExist", "defaultTest");
Assert.assertEquals("defaultTest", value);
}
@Test
@Ignore
public void settingTestForAbsPath(){
public void settingTestForAbsPath() {
//noinspection MismatchedQueryAndUpdateOfCollection
Setting setting = new Setting("d:\\excel-plugin\\other.setting", true);
Console.log(setting.getStr("a"));
}
@Test
public void settingTestForCustom() {
Setting setting = new Setting();
setting.put("group1", "user", "root");
setting.put("group2", "user", "root2");
setting.put("group3", "user", "root3");
setting.set("user", "root4");
Assert.assertEquals("root", setting.getByGroup("user", "group1"));
Assert.assertEquals("root2", setting.getByGroup("user", "group2"));
Assert.assertEquals("root3", setting.getByGroup("user", "group3"));
Assert.assertEquals("root4", setting.get("user"));
}
/**
* 测试写出是否正常
*/
@Test
public void storeTest() {
Setting setting = new Setting("test.setting");
setting.set("testKey", "testValue");
setting.store();
}
}