Properties资源文件类总结
平时的开发中可能会遇到对资源文件操作的情况,平时对资源文件类进行了一下总结:
1、Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
一个属性列表可包含另一个属性列表作为它的“默认值”;如果未能在原有的属性列表中搜索到属性键,则搜索第二个属性列表。
因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法。但强烈反对使用这两个方法,因为它们允许调用方插入其键或值不是 Strings 的项。相反,应该使用 setProperty 方法。如果在“有危险”的 Properties 对象(即包含非 String 的键或值)上调用 store 或 save 方法,则该调用将失败。
load 和 store 方法按下面所指定的、简单的面向行的格式加载和存储属性。此格式使用 ISO 8859-1 字符编码。可以使用 Unicode 转义符来编写此编码中无法直接表示的字符;转义序列中只允许单个 'u' 字符。可使用 native2ascii 工具对属性文件和其他字符编码进行相互转换。
loadFromXML(InputStream) 和 storeToXML(OutputStream, String, String) 方法按简单的 XML 格式加载和存储属性。默认使用 UTF-8 字符编码,但如果需要,可以指定某种特定的编码。XML 属性文档具有以下 DOCTYPE 声明:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
注意,导入或导出属性时不 访问系统 URI (http://java.sun.com/dtd/properties.dtd);该系统 URI 仅作为一个惟一标识 DTD 的字符串:
2、以上文字摘自官方API文档,现对Properties类的几个常用方法进行介绍:
(1)String getProperty(String key)
用指定的键在此属性列表中搜索属性。如果在此属性列表中未找到该键,则接着递归检查默认属性列表及其默认值。如果未找到属性,则此方法返回 null。
(2)String getProperty(String key, String defaultValue)
用指定的键在属性列表中搜索属性。如果在属性列表中未找到该键,则接着递归检查默认属性列表及其默认值。如果未找到属性,则此方法返回默认值变量。
(3)void list(PrintStream out)
将属性列表输出到指定的输出流
(4)void list(PrintWriter out)
将属性列表输出到指定的输出流。
(5)void load(InputStream inStream)
从输入流中读取属性列表(键和元素对)。
(6)Enumeration<?> propertyNames()
返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。
(7)Object setProperty(String key,String value)
调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
(8)void store(OutputStream out,String comments)
以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。
(9)loadFromXML(InputStream in)
将指定输入流中由 XML 文档所表示的所有属性加载到此属性表中。
详细介绍请查阅相关资料。
3、以下是我总结的资源文件工具类import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Properties;/** * 通用资源文件解析类. * Universal resource file parsing class. * * @author Hetal * */ public class ReadProperties { private Properties pro = null; private InputStream in = null; private String proValue = ""; /** * 根据资源文件所在路径和key获取value,用于获取资源文件中指定key的value值。 * Resource files are located under the path and key for value * * @param proPath * @param proKey * @return */ public String getPropertiesValue(String proPath, String proKey){ try { pro = new Properties(); in = ReadProperties.class.getResourceAsStream(proPath); pro.load(in); proValue = pro.getProperty(proKey); } catch (IOException e) { e.printStackTrace(); } return proValue; } /** * 获取资源文件的key集合,用户获取资源文件中所有的key。 * The key for the resource file collection * * @param proPath * @return */ public List<String> getAllKeyList(String proPath) { List<String> keyList = new ArrayList<String>(); try { pro = new Properties(); in = ReadProperties.class.getResourceAsStream(proPath); pro.load(in); Enumeration<?> keyEnum = pro.propertyNames(); while(keyEnum.hasMoreElements()){ keyList.add(keyEnum.nextElement().toString()); } } catch (IOException e) { e.printStackTrace(); } return keyList; } /** * 判断传入的key在资源文件中是否已经存在 * * @param proPath * @param proKey * @return */ public boolean isKeyExist(String proPath, String proKey){ boolean keyExist = false; try { pro = new Properties(); in = ReadProperties.class.getResourceAsStream(proPath); pro.load(in); Enumeration<?> keyEnum = pro.propertyNames(); while(keyEnum.hasMoreElements()){ String existKey = keyEnum.nextElement().toString(); if(proKey.equals(existKey)){ keyExist = true; return keyExist; } } } catch (IOException e) { e.printStackTrace(); } return keyExist; } /** * 用于向资源文件中写指定key和vlaue的资源信息。 * Write to the resource file resource information * * @param proPath 资源文件路径 * @param proKey 资源名称 * @param proValue 资源内容 */ public void writeProperties(String proPath, String proKey, String proValue) throws Exception{ pro = new Properties(); in = ReadProperties.class.getResourceAsStream(proPath); pro.load(in); OutputStream outs = new FileOutputStream(proPath); //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。 pro.setProperty(proKey, proValue); //以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流 pro.store(outs, null); outs.flush(); } /** * 根据xml文件路径和key获取value * According to access xml file path and key value * * @param xmlPath xml文件路径 * @param xmlKey 要获取的key名称 * @return */ public String getPropertiesFromXML(String xmlPath, String xmlKey){ try { pro = new Properties(); in = ReadProperties.class.getResourceAsStream(xmlPath); pro.loadFromXML(in); proValue = pro.getProperty(xmlKey); } catch (IOException e) { e.printStackTrace(); } return proValue; } }