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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM