获取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