您的位置:首页 > 运维架构

加载properties属性文档常用的三种方式

2017-07-25 23:25 99 查看
假设c3p0.properties在src的根目录下。
//1.通过File类去加载properties文件(只适用于普通的Java工程)。
Properties prop = new Properties();
File src = new File("src/c3p0.properties");
prop.load(new FileInputStream(src));
System.out.println(prop.getProperty("c3p0.user"));
//2.通过类加载器去加载properties文件
Properties prop = new Properties();
InputStream in = MyDBUtils.class.getClassLoader().getResourceAsStream("c3p0.properties");
prop.load(in);
System.out.println(prop.getProperty("c3p0.user"));
//3.使用ResourceBundle加载properties文档ResourceBundle resource = ResourceBundle.getBundle("c3p0");//注意:无需加扩展名properties
System.out.println(resource.getString("c3p0.user"));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: