把設定檔以json字串格式儲存,並讀取放到一個map中

來源:互聯網
上載者:User

標籤: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中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.