JAVA读取属性文件的几种方法总结
1.使用java.util.Properties类的load()方法
示例:
Java代码
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2.使用java.util.ResourceBundle类的getBundle()方法
示例:
Java代码
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3.使用java.util.PropertyResourceBundle类的构造函数
示例:
Java代码
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4.使用class变量的getResourceAsStream()方法
示例:
Java代码
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
Java代码
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
Java代码
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
7.使用apache的PropertiesConfiguration类
示例:
Java代码
Configuration config = new PropertiesConfiguration("test.properties");
config.getProperty(key);
相关文章
BiConsumer接口中的方法andThen accept使用详解
这篇文章主要为大家介绍了BiConsumer接口中的方法andThen accept使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07Lombok之@AllArgsConstructor的使用方式
这篇文章主要介绍了Lombok之@AllArgsConstructor的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-09-09Java中使用jaxp进行sax解析_动力节点Java学院整理
使用SAX的优势在于其解析速度较快,相对于DOM而言占用内存较少。这篇文章主要介绍了Java中使用jaxp进行sax解析,需要的朋友可以参考下2017-08-08idea2020.1设置多个spring boot的service启动的实现
这篇文章主要介绍了idea2020.1设置多个spring boot的service启动,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-06-06Spring Data JPA实现分页Pageable的实例代码
本篇文章主要介绍了Spring Data JPA实现分页Pageable的实例代码,具有一定的参考价值,有兴趣的可以了解一下2017-07-07
最新评论