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

来源:互联网 发布:淘宝试衣服模特叫什么 编辑:程序博客网 时间:2024/05/18 12:37
假设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");//注意:无需加扩展名propertiesSystem.out.println(resource.getString("c3p0.user"));
原创粉丝点击