mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add store method
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user