Properties入门使用

java.util.Properties

1.java代码

外部sample.properties文件中:

#以下为服务器、数据库信息 (自动生成)

foo=bar
fu=baz

#其它信息等(自动生成)

内部JAVA

Properties pp = new Properties();

File f = new File("E:\\purple\\2012\\Jan\\20120113\\sample.properties");

try {

FileInputStream fis = new FileInputStream(f);

pp.load(fis);//装载

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(pp.get("fu"));

System.out.println(pp.getProperty("foo"));//两种方法都可以,参数是KEY

pp.list(System.out);//输出如下:(Properties 类在一个散列表(hashtable,事实上是一个 Hashtable 子类)中储存一组键-值对,所以不能保证顺序。 )

/*-- listing properties --

fu=baz

foo=bar */

2. XML 版本的属性文件

外部sampleprops.xml文件中:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  

<properties>  

<comment>Hi</comment>  

<entry key="foo">bar</entry>  

<entry key="fu">baz</entry>  

</properties>  

内部JAVA

Properties pp = new Properties();

File f2 = new File("E:\\purple\\2012\\Jan\\20120113\\sampleprops.xml");

try {

FileInputStream fis = new FileInputStream(f);

pp.loadFromXML(fis);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (InvalidPropertiesFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(pp.get("fu"));

System.out.println(pp.getProperty("foo"));//两种方法都可以,参数是KEY

pp.list(System.out); //输出如下:

/*-- listing properties --

fu=baz

foo=bar*/

3.保存 XML 属性

新的 Properties 还有一个功能是将属性存储到 XML 格式的文件中。虽然 store() 方法仍然会创建一个Properties格式的文件,但是现在可以用新的 storeToXML() 方法创建XML格式的文件。只要传递一个 OutputStream 和一个用于注释的 String 就可以了。以下展示了新的 storeToXML() 方法。

内部JAVA

Properties pp = new Properties();

File f3 = new File("E:\\purple\\2012\\Jan\\20120113\\sampleprops2.xml");

try {

FileOutputStream fos = new FileOutputStream(f);

pp.setProperty("k", "v");

pp.setProperty("e", "a");

pp.setProperty("y", "l");

pp.storeToXML(fos, "Rhyme");//参数"Rhyme"随意

fos.flush();

fos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

外部sampleprops2.xml文件自动创建,内容中:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>Rhyme</comment>

<entry key="k">v</entry>

<entry key="e">a</entry>

<entry key="y">l</entry>

</properties>

//若输出为Properties格式的文件,则修改两句话:

File f3 = new File("E:\\purple\\2012\\Jan\\20120113\\sampleprops2. properties");

pp.store (fos, "Rhyme");

4. 读取properties的全部信息:Enumeration

Properties props = new Properties();
try {
    InputStream in = new BufferedInputStream (new FileInputStream(filePath));
    props.load(in);
    Enumeration en = props.propertyNames();

//propertyNames()返回属性列表中所有键的枚举
    while (en.hasMoreElements()) {
        String key = (String) en.nextElement();
        String Property = props.getProperty (key);
        System.out.println(key+Property);
    }
} catch (Exception e) {
e.printStackTrace();
}

5.使用J2SE API读取Properties文件的六种方法


1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoadergetResourceAsStream()方法
示例:

 InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充
Servlet中可以使用javax.servlet.ServletContextgetResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

posted @ 2013-10-21 11:01  tvxqpurpleline  阅读(212)  评论(0编辑  收藏  举报