把配置文件以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);
}
}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。