java 读取外部和source下配置文件

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Map; import org.bouncycastle.crypto.RuntimeCryptoException;
import org.yaml.snakeyaml.Yaml; public class DbpTestConfig {
private static String dbpIP; private static String dbpPort; private static String testServerIP; private static String testServerPort; static{
Yaml yaml = new Yaml();
try {
        //读取当前项目同级目录下的配置文件,jar包形式
File dumpFile=new File(System.getProperty("user.dir") + "/application.yml");
if(!dumpFile.exists()){
          //读取source目录下配置文件
URL url = DbpTestConfig.class.getClassLoader().getResource("application.yml");
if(url==null){
throw new RuntimeException("not found application.yml");
}
dumpFile=new File(url.getPath());
} Map map =(Map)yaml.load(new FileInputStream(dumpFile));
Map<String,String> paramMap =(Map) map.get("dbptest");
if(paramMap==null){
throw new RuntimeException("not found parameter dbptest");
}
dbpIP=paramMap.get("dbpIP");
dbpPort=String.valueOf(paramMap.get("dbpPort"));
testServerIP=paramMap.get("testServerIP");
testServerPort=String.valueOf(paramMap.get("testServerPort")); } catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} public static String getDbpIP() {
return dbpIP;
} public static String getDbpPort() {
return dbpPort;
} public static String getTestServerIP() {
return testServerIP;
} public static String getTestServerPort() {
return testServerPort;
} }
上一篇:Python 3 学习笔记


下一篇:向架构师进军--->系统架构设计基础知识