首页 > 代码库 > 把配置文件以json字符串格式保存,并读取放到一个map中
把配置文件以json字符串格式保存,并读取放到一个map中
1******************************* txt文件platform.txt
{"sls":{
"domobKey","123456",
"signPassword":"sls_api_20190914",
"qqCoinsKey":"sls_api_20190914"
}, "zonggou":{
"domobKey","123456",
"signPassword":"sls_api_20190914",
"qqCoinsKey":"sls_api_20190914"
}, "zhihui":{
"domobKey","123456",
"signPassword":"sls_api_20190914",
"qqCoinsKey":"sls_api_20190914"
}
}
2 *************************** Platform.java文件
package com.weixin.pojo;
import java.io.Serializable;
public class Platform implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3987351091823568051L;
private String signPassword;
private String qqCoinsKey;
private String domobKey;
public String getDomobKey() {
return domobKey;
}
public void setDomobKey(String domobKey) {
this.domobKey = domobKey;
}
public String getQqCoinsKey() {
return qqCoinsKey;
}
public void setQqCoinsKey(String qqCoinsKey) {
this.qqCoinsKey = qqCoinsKey;
}
public String getSignPassword() {
return signPassword;
}
public void setSignPassword(String signPassword) {
this.signPassword = signPassword;
}
3 ************************PlatformUtils.java文件
package com.weixin.common;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.reflect.TypeToken;
import com.weixin.pojo.Platform;
public class PlatformUtils {
private static final Map<String, Platform> map = new HashMap<String, Platform>();
protected static final Logger log = LogManager
.getLogger(PlatformUtils.class);
public static Platform read(String platform) {
return map.get(platform);
}
static {
try {
InputStream is = PlatformUtils.class.getClassLoader()
.getResourceAsStream("platform.txt");
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(is);
while (scanner.hasNext()) {
sb.append(scanner.next());
}
Map<String, Platform> result = CommonUtil.gson().fromJson(
sb.toString(), new TypeToken<Map<String, Platform>>() {
private static final long serialVersionUID = 3475626220958000879L;
}.getType());
map.putAll(result);
scanner.close();
} catch (Exception e) {
log.error("读取出错{}", e);
}
}
}
把配置文件以json字符串格式保存,并读取放到一个map中