读取配置文件的两种方式

方法一: 通过类加载器的方式读取配置文件

  1.   Properties properties=new Properties();  
  2.     InputStream is=Demo1.class.getClassLoader().getResourceAsStream("com/nicai/test.properties");  
  3.     properties.load(is);  
  4.     System.out.println(properties.getProperty("key1")); 

com/nicai/test.properties  前面不能 有 /

一般放在 静态代码块 内 只需执行一次

//第二种方法,读取配置文件
ResourceBundle rb = ResourceBundle.getBundle("config");//基名 就是不带扩展名
String daoClassName = rb.getString("dao");//直接取Key对应的value

原文地址:https://www.cnblogs.com/weixiaozhekan/p/5167688.html