獲取properties中對應的值


import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;
public class PropertiesUtil {
  public static final String FILE_PATH = "jdbc.properties";

  /**
  * .properties 文件返回 Properties對象
  * @return
  */
  public static Properties getProperties(){
    Properties properties = new Properties();
    try{
      properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(FILE_PATH));
    }catch (IOException e){
      throw new RuntimeException("File Read Failed...", e);
    }
    return properties;
  }

  /**
  * 測試 獲取jdbc.properties 內容
  */
  public static void main(String[] args) {
    //方法一:調用上述方法獲取
    String sqlserverclassname1 = getProperties().getProperty("sqlserverclassname");
    //方法二:如下
    String sqlserverclassname2 = ResourceBundle.getBundle("jdbc").getString("sqlserverclassname");
    System.out.println("sqlserverclassname1=="+sqlserverclassname1);
    System.out.println("sqlserverclassname2=="+sqlserverclassname2);
  }
}

打印結果:

 


免責聲明!

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



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