- mport java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- /**解析xxx.properties文件
- * 并假定使用 ISO 8859-1 字符编码;即每个字节都是 Latin1 字符
- * 对于非 Latin1 的字符和某些特殊字符,可以使用 Unicode 转义以键和元素的形式来表示它们
- * 配置文件格式:
- * 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)
- * 如:键为Truth,值为Beauty
- * Truth = Beauty
- * Truth = Beauty
- * Truth : Beauty
- * 键为fruits,值为apple,banana,pear,cantaloupe,watermelon,kiwi,mango
- * fruits:
- * apple,banana,pear,/
- * cantaloupe,watermelon,/
- * kiwi,mango
- * 注释行以 ASCII 字符 '#' 或 '!' 作为开头,不参加解析
- * @author guwh
- *
- */
- public class TestParseProperties {
- private static TestParseProperties parseProperties;
- //获取java.util.Properties类
- Properties properties = new Properties();
- private TestParseProperties() {
- try {
- this.parseProp();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static TestParseProperties getInstance() {
- if (parseProperties == null)
- parseProperties = new TestParseProperties();
- return parseProperties;
- }
- public Properties parseProp() throws IOException {
- /**
- * Class.getResourceAsStream(String name)
- * 查找具有给定名称的资源,return 一个 InputStream 对象
- * 如果找不到带有该名称的资源,则返回 null
- * NullPointerException - 如果 name 是 null
- */
- /**
- * 配置文件jwt.properties在包src.com.jabberchina.xmppserver.plugin.jwt.vo下
- InputStream is = this.getClass().getResourceAsStream("/com/jabberchina/xmppserver/plugin/jwt/vo/jwt.properties");
- */
- /**
- * 配置文件jwt.properties1与本类TestParseProperties在同一包目录src.test下
- InputStream is = this.getClass().getResourceAsStream("jwt.properties1");
- */
- //配置文件jwt2.properties在包src.test.property下
- InputStream is = this.getClass().getResourceAsStream("property/jwt2.properties");
- /**
- * Properties.load(InputStream inStream)从输入流中读取属性列表(键和元素对)
- * IOException - 如果读取输入流时发生错误。
- * IllegalArgumentException - 如果输入流包含错误的 Unicode 转义序列
- * 此方法返回后,指定的流(inStream)仍保持打开状态,所以若不再使用inStream要手动关闭
- * 返回类型void
- */
- properties.load(is);
- is.close();
- return properties;
- }
- public String getProperties(String key) {
- /**
- *用指定的键在此属性列表中搜索属性
- *如果在此属性列表中未找到该键,则接着递归检查默认属性列表及其默认值,如果未找到属性,则此方法返回 null
- *返回类型String
- */
- return properties.getProperty(key);
- }
- public static void main(String[] args) {
- String key = "dadaPath";
- String restult = TestParseProperties.getInstance().getProperties(key);
- System.out.println("proties value is "+restult);
- }
- }
当然 property也可以读取xml配置文件方法为 property.loadFromXML(InputStream inputStream)
- /**
- * @author guwh
- * @version 创建时间:2011-4-2 下午03:53:10
- * 类说明
- */
- package test;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Properties;
- import org.apache.commons.io.FilenameUtils;
- /**
- * XML 属性文档具有以下 DOCTYPE 声明:
- * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
- * 如:
- * <?xml version="1.0" encoding="UTF-8"?>
- *<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
- *<properties>
- *<entry key="pinyin">hehe</entry>
- *<entry key="name">呵呵</entry>
- *</properties>
- * @author guwh
- *
- */
- public class TestParseXML {
- private static TestParseXML parseXML;
- Properties properties = new Properties();
- private TestParseXML(){
- try{
- this.parseXML();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public static TestParseXML getInstance(){
- if(parseXML == null)
- parseXML = new TestParseXML();
- return parseXML;
- }
- public Properties parseXML() throws IOException{
- String filePath = "D:/";
- String filename = "test.xml";
- /**
- * 以绝对路径方式从磁盘上读取
- *
- */
- BufferedInputStream inBuff=new BufferedInputStream(new FileInputStream(filePath+filename));
- /**
- * 以相对路径方式从工程中读取
- * String filename = "test.xml";
- * InputStream inBuff = this.getClass().getResourceAsStream(filename);
- */
- if("xml".equalsIgnoreCase(FilenameUtils.getExtension(filename)))
- properties.loadFromXML(inBuff);
- inBuff.close();
- return properties;
- }
- public String gerXMLValue(String key){
- return properties.getProperty(key);
- }
- public static void main(String[] args) {
- String key = "pinyin";
- String restult = TestParseXML.getInstance().gerXMLValue(key);
- System.out.println("gerXMLValue value is "+restult);
- }
- }
-
注意xml配置文件中的encoding="UTF-8"要与实际文件中的编码方式一致,否则会报错
二:利用ResourceBundle类读取配置文件此类多用于解析国际化文件
- import java.util.PropertyResourceBundle;
- import java.util.ResourceBundle;
- public class TestResourceBundleParse {
- private TestResourceBundleParse() {
- }
- private static PropertyResourceBundle prb;
- static {
- /**
- * name 为完全限定名(即包名+文件/类名)
- */
- String name = "com.jabberchina.xmppserver.plugin.jwt.util.jwt1";
- prb = (PropertyResourceBundle) ResourceBundle.getBundle(name);
- }
- public static final String getString(String propertyName) {
- return prb.getString(propertyName);
- }
- public static void main(String[] args) {
- String key = "dadaPath";
- String restult = TestResourceBundleParse.getString(key);
- System.out.println("proties value is "+restult);
- }
- }
注意:
以上方法中读文件时,文件名(即文件路径的书写)是不同的,在方法一中获得InputStream流时若通过Class.getResourceAsStream(String filename)获得,则filename为相对路径+文件名;若通过new FileInputStream(String filename)的其他new一个流的方式获得InputStream流则filename为绝对路径+文件名,即磁盘中实际路径+文件名。而二中通过ResourceBundle.getBundle(String name)的方式获得PropertyResourceBundle对象时,name为完全限定名,需注意!