- mportjava.io.IOException;
- importjava.io.InputStream;
- importjava.util.Properties;
- /**解析xxx.properties文件
- *并假定使用ISO8859-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字符'#'或'!'作为开头,不参加解析
- *@authorguwh
- *
- */
- publicclassTestParseProperties{
- privatestaticTestParsePropertiesparseProperties;
- //获取java.util.Properties类
- Propertiesproperties=newProperties();
- privateTestParseProperties(){
- try{
- this.parseProp();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- publicstaticTestParsePropertiesgetInstance(){
- if(parseProperties==null)
- parseProperties=newTestParseProperties();
- returnparseProperties;
- }
- publicPropertiesparseProp()throwsIOException{
- /**
- *Class.getResourceAsStream(Stringname)
- *查找具有给定名称的资源,return一个InputStream对象
- *如果找不到带有该名称的资源,则返回null
- *NullPointerException-如果name是null
- */
- /**
- *配置文件jwt.properties在包src.com.jabberchina.xmppserver.plugin.jwt.vo下
- InputStreamis=this.getClass().getResourceAsStream("/com/jabberchina/xmppserver/plugin/jwt/vo/jwt.properties");
- */
- /**
- *配置文件jwt.properties1与本类TestParseProperties在同一包目录src.test下
- InputStreamis=this.getClass().getResourceAsStream("jwt.properties1");
- */
- //配置文件jwt2.properties在包src.test.property下
- InputStreamis=this.getClass().getResourceAsStream("property/jwt2.properties");
- /**
- *Properties.load(InputStreaminStream)从输入流中读取属性列表(键和元素对)
- *IOException-如果读取输入流时发生错误。
- *IllegalArgumentException-如果输入流包含错误的Unicode转义序列
- *此方法返回后,指定的流(inStream)仍保持打开状态,所以若不再使用inStream要手动关闭
- *返回类型void
- */
- properties.load(is);
- is.close();
- returnproperties;
- }
- publicStringgetProperties(Stringkey){
- /**
- *用指定的键在此属性列表中搜索属性
- *如果在此属性列表中未找到该键,则接着递归检查默认属性列表及其默认值,如果未找到属性,则此方法返回null
- *返回类型String
- */
- returnproperties.getProperty(key);
- }
- publicstaticvoidmain(String[]args){
- Stringkey="dadaPath";
- Stringrestult=TestParseProperties.getInstance().getProperties(key);
- System.out.println("protiesvalueis"+restult);
- }
- }
当然 property也可以读取xml配置文件方法为 property.loadFromXML(InputStream inputStream)
- /**
- *@authorguwh
- *@version创建时间:2011-4-2下午03:53:10
- *类说明
- */
- packagetest;
- importjava.io.BufferedInputStream;
- importjava.io.FileInputStream;
- importjava.io.IOException;
- importjava.util.Properties;
- importorg.apache.commons.io.FilenameUtils;
- /**
- *XML属性文档具有以下DOCTYPE声明:
- *<!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd">
- *如:
- *<?xmlversion="1.0"encoding="UTF-8"?>
- *<!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd">
- *<properties>
- *<entrykey="pinyin">hehe</entry>
- *<entrykey="name">呵呵</entry>
- *</properties>
- *@authorguwh
- *
- */
- publicclassTestParseXML{
- privatestaticTestParseXMLparseXML;
- Propertiesproperties=newProperties();
- privateTestParseXML(){
- try{
- this.parseXML();
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- publicstaticTestParseXMLgetInstance(){
- if(parseXML==null)
- parseXML=newTestParseXML();
- returnparseXML;
- }
- publicPropertiesparseXML()throwsIOException{
- StringfilePath="D:/";
- Stringfilename="test.xml";
- /**
- *以绝对路径方式从磁盘上读取
- *
- */
- BufferedInputStreaminBuff=newBufferedInputStream(newFileInputStream(filePath+filename));
- /**
- *以相对路径方式从工程中读取
- *Stringfilename="test.xml";
- *InputStreaminBuff=this.getClass().getResourceAsStream(filename);
- */
- if("xml".equalsIgnoreCase(FilenameUtils.getExtension(filename)))
- properties.loadFromXML(inBuff);
- inBuff.close();
- returnproperties;
- }
- publicStringgerXMLValue(Stringkey){
- returnproperties.getProperty(key);
- }
- publicstaticvoidmain(String[]args){
- Stringkey="pinyin";
- Stringrestult=TestParseXML.getInstance().gerXMLValue(key);
- System.out.println("gerXMLValuevalueis"+restult);
- }
- }
-
注意xml配置文件中的encoding="UTF-8"要与实际文件中的编码方式一致,否则会报错
二:利用ResourceBundle类读取配置文件此类多用于解析国际化文件
- importjava.util.PropertyResourceBundle;
- importjava.util.ResourceBundle;
- publicclassTestResourceBundleParse{
- privateTestResourceBundleParse(){
- }
- privatestaticPropertyResourceBundleprb;
- static{
- /**
- *name为完全限定名(即包名+文件/类名)
- */
- Stringname="com.jabberchina.xmppserver.plugin.jwt.util.jwt1";
- prb=(PropertyResourceBundle)ResourceBundle.getBundle(name);
- }
- publicstaticfinalStringgetString(StringpropertyName){
- returnprb.getString(propertyName);
- }
- publicstaticvoidmain(String[]args){
- Stringkey="dadaPath";
- Stringrestult=TestResourceBundleParse.getString(key);
- System.out.println("protiesvalueis"+restult);
- }
- }
注意:
以上方法中读文件时,文件名(即文件路径的书写)是不同的,在方法一中获得InputStream流时若通过Class.getResourceAsStream(String filename)获得,则filename为相对路径+文件名;若通过new FileInputStream(String filename)的其他new一个流的方式获得InputStream流则filename为绝对路径+文件名,即磁盘中实际路径+文件名。而二中通过ResourceBundle.getBundle(String name)的方式获得PropertyResourceBundle对象时,name为完全限定名,需注意!