JAVA读取属性文件的几种方法总结
时间:2023-10-21 04:36:40
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);
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ViewPager实现图片轮翻效果
Java 多线程传值的四种方法
![](https://img.aspxhome.com/file/2023/4/79944_0s.png)
浅谈Java多进程程序的运行模式
C#值类型和引用类型的深入理解
![](https://img.aspxhome.com/file/2023/9/98279_0s.png)
C#Process的OutputDataReceived事件不触发问题及解决
Android自定义Adapter的ListView的思路及代码
![](https://img.aspxhome.com/file/2023/6/139356_0s.jpg)
C#中添加窗口的步骤详解
![](https://img.aspxhome.com/file/2023/6/91256_0s.jpg)
一文带你学会Spring JDBC的使用
![](https://img.aspxhome.com/file/2023/7/60377_0s.jpg)
Android webview打开本地图片上传实现代码
Java线程并发中常见的锁机制详细介绍
![](https://img.aspxhome.com/file/2023/9/70339_0s.png)
Spring Boot2.3 新特性分层JAR的使用
![](https://img.aspxhome.com/file/2023/2/65602_0s.jpg)
JVM类加载机制原理及用法解析
![](https://img.aspxhome.com/file/2023/3/68173_0s.jpg)
c#如何实现接口事件
使用监听器对Spring bean id进行唯一校验过程解析
Java StringBuilder和StringBuffer源码分析
Android虚拟机Dalvik和ART科普
![](https://img.aspxhome.com/file/2023/9/138799_0s.png)
ExpandableListView实现二级列表购物车
![](https://img.aspxhome.com/file/2023/4/90154_0s.jpg)
一文搞懂C#实现读写文本文件中的数据
![](https://img.aspxhome.com/file/2023/8/87048_0s.png)
springboot自动配置原理解析
![](https://img.aspxhome.com/file/2023/9/84789_0s.png)