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);
}
}
打印结果: