Properties 持久的属性集的实例详解(propertities)太疯狂了

随心笔谈9个月前发布 admin
173 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

这篇文章介绍了一种通过使用Properties类从文件中读取属性值的方法。文章详细解释了如何使用`readValue`方法读取特定键的值,并通过`readProperties`方法读取所有键值对。文章还提到了如何从输入流中读取属性列表,并使用`BufferedInputStream`来提高读取效率。需要注意的是,文章中提到建议使用ClassLoader加载属性文件,但实际代码中使用了`read`方法而不是`load`方法。文章整体重点在于展示如何高效地读取和处理属性文件。


// 根据key读取value
public void readValue(String filePath, String key) {

Properties props=new Properties();

InputStream in=new BufferedInputStream(new FileInputStream(filePath));
//Thread.currentThread().getContextClassLoader().getResourceAsStream(“eop.properties”);

props.load(in); // 从输入流中读取属性列表(键和元素对)
String value=props.getProperty(key);
}

// 读取properties的全部信息
public static void readProperties(String filePath) {
Properties props=new Properties();
InputStream in=new BufferedInputStream(new FileInputStream(filePath));
props.load(in);

Enumeration en=props.propertyNames();
while (en.hasMoreElements()) {
String key=(String) en.nextElement();
String value=props.getProperty(key);
  }
}

© 版权声明

相关文章