PropertiesLoader使得properties文件的组织更为方便,因为它允许你在properties文件里include别的文件,也允许你用星号“*”来告诉它同时匹配传统的properties文件和新的XML格式properties文件。
PropertiesLoader适合用来载入存放配置信息的properties文件,但是它不能用来替代ResourceBundle,因为它不支持自动选择载入本地化语言资源。它是jabb的一部分。jabb的8号的版本可以从这里下载:http://download.csdn.net/detail/zhengmaohu/3587760以下是使用示范:配置文件
simple.properties
in_simple = this is defined in simple.propertiesinclusion.properties
.include = simple.properties xml.xmlinclusion = This is defined in inclusion.propertiesxml.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<comment></comment>
<entry key="xml attribute">This is defined in xml.xml</entry>
</properties>widecard.properties
widecard_properties = In widecard.propertieswidecard.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<comment></comment>
<entry key="widecard xml">This is defined in widecard.xml</entry>
</properties>multilayer.properties
.include=xml.xml inclusion.properties,
multi=defined in multilayer.propertiesloop.properties
.include=xml.xml inclusion.properties, multilayer.properties
multi=defined in multilayer.properties代码:
public class PropertiesLoaderDemo {
public static void main(String[] args) throws InvalidPropertiesFormatException, IOException {
PropertiesLoader l = new PropertiesLoader(PropertiesLoaderDemo.class);
System.out.println(l.load("simple.properties"));
System.out.println(l.load("xml.xml"));
System.out.println(l.load("inclusion.properties"));
System.out.println(l.load("widecard.*"));
System.out.println(l.load("multilayer.*"));
System.out.println(l.load("loop.*"));
}
}输出:
{in_simple=this is defined in simple.properties}
{xml attribute=This is defined in xml.xml}
{in_simple=this is defined in simple.properties, xml attribute=This is defined in xml.xml, inclusion=This is defined in inclusion.properties}
{widecard xml=This is defined in widecard.xml, widecard_properties=In widecard.properties}
{in_simple=this is defined in simple.properties, xml attribute=This is defined in xml.xml, multi=defined in multilayer.properties, inclusion=This is defined in inclusion.properties}
Exception in thread "main" java.lang.IllegalArgumentException: Loop found when loading properties file: inclusion.properties
at net.sf.jabb.util.prop.PropertiesLoader.load(PropertiesLoader.java:139)
at net.sf.jabb.util.prop.PropertiesLoader.load(PropertiesLoader.java:151)
at net.sf.jabb.util.prop.PropertiesLoader.load(PropertiesLoader.java:151)
at net.sf.jabb.util.prop.PropertiesLoader.load(PropertiesLoader.java:186)
at net.sf.jabb.util.prop.test.PropertiesLoaderDemo.main(PropertiesLoaderDemo.java:22)