Skip to content

Commit

Permalink
add store method
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Sep 10, 2020
1 parent 76c6aee commit d23bdd9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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修复

Expand Down
14 changes: 14 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2441,6 +2441,20 @@ public static PrintWriter getPrintWriter(File file, String charset, boolean isAp
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));
}

/**
* 获取当前系统的换行分隔符
*
Expand Down
37 changes: 36 additions & 1 deletion hutool-setting/src/main/java/cn/hutool/setting/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -227,6 +228,18 @@ public void onModify(WatchEvent<?> event, Path currentPath) {
}

/**
* 获得设定文件的URL
*
* @return 获得设定文件的路径
* @since 5.4.3
*/
public URL getSettingUrl() {
return this.settingUrl;
}

/**
* 获得设定文件的路径
*
* @return 获得设定文件的路径
*/
public String getSettingPath() {
Expand Down Expand Up @@ -334,17 +347,39 @@ public Props getProps(String group) {

// --------------------------------------------------------------------------------- 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>
* 持久化不会保留之前的分组
*
* @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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
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;
Expand Down Expand Up @@ -168,9 +170,22 @@ public void setAssignFlag(char assignFlag) {
* @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void init() {

@Test
public void propTest() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("test.properties");
String user = props.getProperty("user");
Assert.assertEquals(user, "root");
Expand All @@ -41,6 +42,7 @@ public void propTest() {
@Test
@Ignore
public void propTestForAbsPAth() {
//noinspection MismatchedQueryAndUpdateOfCollection
Props props = new Props("d:/test.properties");
String user = props.getProperty("user");
Assert.assertEquals(user, "root");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,63 @@

/**
* 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();
}
}

0 comments on commit d23bdd9

Please sign in to comment.