码迷,mamicode.com
首页 > Web开发 > 详细

把配置文件以json字符串格式保存,并读取放到一个map中

时间:2014-09-24 13:38:03      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   java   for   文件   sp   on   



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中

标签:style   color   io   os   java   for   文件   sp   on   

原文地址:http://blog.csdn.net/wangcomputer2010/article/details/39521059

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!