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; } }

 

转载于:https://www.cnblogs.com/itniwota/p/9320049.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值