This commit is contained in:
Looly
2022-11-13 19:50:05 +08:00
parent a78619502e
commit 7e28a55c75
5 changed files with 121 additions and 25 deletions

View File

@@ -697,7 +697,6 @@ public class Setting extends AbsSetting implements Map<String, String> {
*
* @return 默认分组(空分组)中的所有键列表
*/
@SuppressWarnings("NullableProblems")
@Override
public Set<String> keySet() {
return this.groupedMap.keySet(DEFAULT_GROUP);
@@ -708,7 +707,6 @@ public class Setting extends AbsSetting implements Map<String, String> {
*
* @return 默认分组(空分组)中的所有值列表
*/
@SuppressWarnings("NullableProblems")
@Override
public Collection<String> values() {
return this.groupedMap.values(DEFAULT_GROUP);
@@ -719,7 +717,6 @@ public class Setting extends AbsSetting implements Map<String, String> {
*
* @return 默认分组(空分组)中的所有键值对列表
*/
@SuppressWarnings("NullableProblems")
@Override
public Set<Entry<String, String>> entrySet() {
return this.groupedMap.entrySet(DEFAULT_GROUP);

View File

@@ -23,7 +23,6 @@ import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.log.StaticLog;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
@@ -190,12 +189,7 @@ public final class Props extends Properties implements TypeGetter<CharSequence>
public void load(final Resource resource) {
Assert.notNull(resource, "Props resource must be not null!");
this.resource = resource;
try (final BufferedReader reader = resource.getReader(charset)) {
super.load(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
PropsLoaderUtil.loadTo(this, resource, this.charset);
}
/**

View File

@@ -0,0 +1,61 @@
package cn.hutool.setting.dialect;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Properties;
/**
* {@link Properties} 资源内容加载工具
*
* @author looly
* @since 6.0.0
*/
public class PropsLoaderUtil {
/**
* 加载配置文件内容到{@link Properties}中<br>
* 需要注意的是,如果资源文件的扩展名是.xml会调用{@link Properties#loadFromXML(InputStream)} 读取。
*
* @param properties {@link Properties}文件
* @param resource 资源
* @param charset 编码对XML无效
*/
public static void loadTo(final Properties properties, final Resource resource, final Charset charset) {
final String filename = resource.getName();
if (filename != null && filename.endsWith(".xml")) {
// XML
try (final InputStream in = resource.getStream()) {
properties.loadFromXML(in);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
} else {
// .properties
try (final BufferedReader reader = resource.getReader(charset)) {
properties.load(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
}
/**
* 加载指定名称的所有配置文件内容到{@link Properties}中
*
* @param properties {@link Properties}文件
* @param resourceName 资源名可以是相对classpath的路径也可以是绝对路径
* @param classLoader {@link ClassLoader}{@code null}表示使用默认的当前上下文ClassLoader
* @param charset 编码对XML无效
*/
public static void loadAllTo(final Properties properties, final String resourceName, final ClassLoader classLoader, final Charset charset) {
for (final Resource resource : ResourceUtil.getResources(resourceName, classLoader)) {
loadTo(properties, resource, charset);
}
}
}