Properties 类的使用
Properties 類已不是新東西了,它在 Java 編程的早期就有了,并且幾乎沒有什么變化。J2SE 的 Tiger 版本增強了這個類,不僅可以用它在單獨一行中指定用等號分隔的多個鍵-值對,還可以用XML 文件裝載和保存這些鍵-值對。在 馴服 Tiger的這一期文章中,John Zukowski 展示了如何駕馭這匹新一代的“役馬”。請在本文對應的討論論壇上與作者及其他讀者分享您對本文的想法(您也可以單擊文章頂部或底部的 討論來訪問該論壇)。
? ?? ?J2SE 1.5 以前的版本要求直接使用 XML 解析器來裝載配置文件并存儲設置。雖然這并非是一件困難的事情,并且解析器是平臺的標準部分,但是額外的工作總是有點讓人煩。最近更新的 java.util.Properties 類現在提供了一種為程序裝載和存儲設置的更容易的方法: loadFromXML(InputStream is) 和 storeToXML(OutputStream os, String comment) 方法。
Properties 基本知識
如果不熟悉 java.util.Properties 類,那么現在告訴您它是用來在一個文件中存儲鍵-值對的,其中鍵和值是用等號分隔的,如清單 1 所示。
清單 1. 一組屬性示例
foo=bar
fu=baz
將 清單 1 裝載到 Properties 對象中后,您就可以找到兩個鍵( foo 和 fu )和兩個值( foo 的 bar 和 fu 的 baz )了。這個類支持帶 /u 的嵌入 Unicode 字符串,但是這里重要的是每一項內容都當作 String 。
清單 2 顯示了如何裝載屬性文件并列出它當前的一組鍵和值。只需傳遞這個文件的 InputStream 給 load() 方法,就會將每一個鍵-值對添加到 Properties 實例中。然后用 list() 列出所有屬性或者用 getProperty() 獲取單獨的屬性。
清單 2. 裝載屬性
import java.util.*;
import java.io.*;
public class LoadSample {
??public static void main(String args[]) throws Exception {
? ? Properties prop = new Properties();
? ? FileInputStream fis =
? ?? ?new FileInputStream("sample.properties");
? ? prop.load(fis);
? ? prop.list(System.out);
? ? System.out.println("/nThe foo property: " +
? ?? ???prop.getProperty("foo"));
??}
}
運 行 LoadSample 程序生成如清單 3 所示的輸出。注意 list() 方法的輸出中鍵-值對的順序與它們在輸入文件中的順序不一樣。 Properties 類在一個散列表(hashtable,事實上是一個 Hashtable 子類)中儲存一組鍵-值對,所以不能保證順序。
清單 3. LoadSample 的輸出
-- listing properties --
fu=baz
foo=bar
The foo property: bar
XML 屬性文件
這里沒有什么新內容。 Properties 類總是這樣工作的。不過,新的地方是從一個 XML 文件中裝載一組屬性。它的 DTD 如清單 4 所示。
清單 4. 屬性 DTD
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
如 果不想細讀 XML DTD,那么可以告訴您它其實就是說在外圍 <properties> 標簽中包裝的是一個 <comment> 標簽,后面是任意數量的 <entry> 標簽。對每一個 <entry> 標簽,有一個鍵屬性,輸入的內容就是它的值。清單 5 顯示了 清單 1中的屬性文件的 XML 版本是什么樣子的。
清單 5. 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>
如果清單 6 所示,讀取 XML 版本的 Properties 文件與讀取老格式的文件沒什么不同。
清單 6. 讀取 XML Properties 文件
import java.util.*;
import java.io.*;
public class LoadSampleXML {
??public static void main(String args[]) throws Exception {
? ? Properties prop = new Properties();
? ? FileInputStream fis =
? ?? ?new FileInputStream("sampleprops.xml");
? ? prop.loadFromXML(fis);
? ? prop.list(System.out);
? ? System.out.println("/nThe foo property: " +
? ?? ???prop.getProperty("foo"));
??}
}
關于資源綁定的說明
雖 然 java.util.Properties 類現在除了支持鍵-值對,還支持屬性文件作為 XML 文件,不幸的是,沒有內置的選項可以將 ResourceBundle 作為一個 XML 文件處理。是的, PropertyResourceBundle 不使用 Properties 對象來裝載綁定,不過裝載方法的使用是硬編碼到類中的,而不使用較新的 loadFromXML() 方法。
運行清單 6 中的程序產生與原來的程序相同的輸出,如 清單 2所示。
保存 XML 屬性
新 的 Properties 還有一個功能是將屬性存儲到 XML 格式的文件中。雖然 store() 方法仍然會創建一個類似 清單 1 所示的文件,但是現在可以用新的 storeToXML() 方法創建如 清單 5 所示的文件。只要傳遞一個 OutputStream 和一個用于注釋的 String 就可以了。清單 7 展示了新的 storeToXML() 方法。
清單 7. 將 Properties 存儲為 XML 文件
import java.util.*;
import java.io.*;
public class StoreXML {
??public static void main(String args[]) throws Exception {
? ? Properties prop = new Properties();
? ? prop.setProperty("one-two", "buckle my shoe");
? ? prop.setProperty("three-four", "shut the door");
? ? prop.setProperty("five-six", "pick up sticks");
? ? prop.setProperty("seven-eight", "lay them straight");
? ? prop.setProperty("nine-ten", "a big, fat hen");
? ? FileOutputStream fos =
? ?? ?new FileOutputStream("rhyme.xml");
? ? prop.storeToXML(fos, "Rhyme");
? ? fos.close();
??}
}
運行清單 7 中的程序產生的輸出如清單 8 所示。
清單 8. 存儲的 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Rhyme</comment>
<entry key="seven-eight">lay them straight</entry>
<entry key="five-six">pick up sticks</entry>
<entry key="nine-ten">a big, fat hen</entry>
<entry key="three-four">shut the door</entry>
<entry key="one-two">buckle my shoe</entry>
</properties>
結束語
使 用 XML 文件還是使用老式的 a=b 類型的文件完全取決于您自己。老式文件從內存的角度看肯定是輕量級的。不過,由于 XML 的普遍使用,人們會期望 XML 格式流行起來,因為它已經被廣泛使用了,只不過沒有用到 Properties 對象。選擇完全在您。分析軟件包 private XMLUtils 類的源代碼以獲得關于所使用的 XML 解析的更多信息。
?
?
?
PTest.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
?* 實現properties文件的讀取
?* @author bbflyerwww
?* @date 2006-08-02
?*/
public class PTest {
??? public static void main(String[] args) {
??????? try {
??????????? long start = System.currentTimeMillis();
??????????? InputStream is = new FileInputStream("conf.properties");
??????????? Properties p = new Properties();
??????????? p.load(is);
??????????? is.close();
??????????? System.out.println("SIZE : " + p.size());
??????????? System.out.println("homepage : " + p.getProperty("homepage"));
??????????? System.out.println("author : " + p.getProperty("author"));
??????????? System.out.println("school : " + p.getProperty("school"));
??????????? System.out.println("date : " + p.getProperty("date"));
??????????? long end = System.currentTimeMillis();?
??????????? System.out.println("Cost : " + (end - start));
??????? } catch (IOException ioe) {
??????????? ioe.printStackTrace();
??????? }
??? }
}
conf.properties
# Configuration file
homepage = http://hi.baidu.com/bbflyerwww
author = bbflyerwww
school = WuHan University
date = 2006-08-02
Result
SIZE:4
homepage : http://hi.baidu.com/bbflyerwww
author : bbflyerwww
school : WuHan University
date : 2006-08-02
Cost : 0
在做程序時,往往會有一些固定的或長期的值,這些值或許在將來才會被更改。
??? 由于程序完成時,把這些值寫在代碼,將來更改起不方便。(而且容易忘記在代碼中什么地方,哪個文件中時)所以,我們建議將這些值寫入配置文件中。
??? 今天就讓我們一起來看看,在 JAVA 中對 properties 這種配置文件的操作吧。
1、properties 文件
#ParseZUrlNum.properties
#Tue Nov 15 11:52:15 CST 2005
nextnum=360
firstnum=73
secondnum=72
2、讀寫文件
{
ParseNZ pnz = new ParseNZ();
Properties properties = new Properties();
try {
??????????? File f = new File("ParseZUrlNum.properties");
??????????? properties = pnz.getProperties ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
} catch (Exception e) {
??????????? e.printStackTrace();
}
String firstnum = properties.getProperty("firstnum");
String secondnum = properties.getProperty("secondnum");
String nextnum = properties.getProperty("nextnum");
int first=Integer.parseInt(firstnum);
int second=Integer.parseInt(secondnum);
int next=Integer.parseInt(nextnum);
System.out.println("firstnum=" + firstnum);
System.out.println("secondnum=" + secondnum);
System.out.println("nextnum=" + nextnum);
pnz.setProperties(first,second,next);
}
?
3、ParseNZ 類
public class ParseNZ{
??? Properties p = new Properties();
??? public Properties getProperties(String filename) throws IOException {
??????? ClassLoader cl = this.getClass().getClassLoader();
??????? FileInputStream input;
??????? // if(cl!=null)
??????? //input=cl.getResourceAsStream(filename);
??????? input = new FileInputStream(filename);
??????? //else
??????? //input=ClassLoader.getSystemResourceAsStream(filename);
??????? p.load(input);
??????? return p;
??? }
???
??? public void setProperties(int first, int second, int next) {
??????? p.setProperty("firstnum",String.valueOf(first));
??????? p.setProperty("secondnum",String.valueOf(second));
??????? p.setProperty("nextnum",String.valueOf(next));
??????? File file = new File ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
??????? try {
????????????? OutputStream fos = new FileOutputStream(file);
????????????? p.store(fos, "ParseZUrlNum.properties");
????????????? fos.close();
??????? } catch (FileNotFoundException ex) {
??????????? System.out.println("file is NULL !!!");
????????????? ex.printStackTrace();
??????? } catch (IOException ex) {
??????????? System.out.println("IO is Error !!!");
????????????? ex.printStackTrace();
??????? }
??? }
}
??? 本例中地址是寫死的。可改為如:?File file = new File(System.getProperty("user.dir")
???????????????????????????????? + "/EMweb/WEB-INF/admininfo.properties");
這里面的關鍵是System.getProperty("user.dir") 返回當前工作目錄。
轉載于:https://www.cnblogs.com/dainiao01/archive/2008/05/20/2250309.html
總結
以上是生活随笔為你收集整理的Properties 类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对象结构
- 下一篇: BulkLoader类使用