springBoot中同时获取key及value值存入map中,配置文件(文件在resource下)如下:
=19931216
=19940912
=20210505
加载配置工具类:
package ;
import ;
import ;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* @author candy
* @version 1.0
* @createDate 2020/5/16 22:58
* @description 获取文件配置信息
*/
public class PropertiesUtils {
protected Logger logger = (getClass());
public Properties properties;
private String[] resourcesPaths;
// 无参构造
public PropertiesUtils() {
}
// 传入配置文件名称,可以多个
public PropertiesUtils(String... resourcesPaths) {
load(resourcesPaths);
}
// 加载配置文件信息
private void load(String[] resourcesPaths) {
= resourcesPaths;
properties = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
is = new FileInputStream(getAbsolutePath(location));
(is);
} catch (IOException ex) {
("Could not load properties from path:" + location + ", " + ());
} catch (Exception ex) {
("Could not load properties from path:" + location + ", " + ());
} finally {
(is);
}
}
}
// 获取根据key获取value
public Object getValue(String key) {
if ((key)) {
return (key);
}
return "";
}
// 获取文件绝对路径
public String getAbsolutePath(String filename) throws URISyntaxException {
if (!(filename)) {
filename = ().getResource(filename).toURI().getPath();
}
return filename;
}
public static void main(String[] args) {
long startTime = ();
PropertiesUtils properties = new PropertiesUtils("", "");
Iterator<Entry<Object, Object>> map = ().iterator();
while (()) {
Entry<Object, Object> entry = (); // 遍历获取key value
Object key = ();
Object value = ();
(key + "=" + value);
}
("load time :" + (() - startTime) + "ms");
}
}
如果配置文件key或value含有中文,可以把中文转为ASCII代码中获取到的就是中文(否则会乱码,ASCII与中文互转网站点此),如下:
=19931216
=19940912
=20210505
# 百度=/
\u767e\u5ea6=/
# 网易=https:///
\u7f51\u6613=https:///