aaa=1\
11
b
bb = 222
aaa=111
bbb=222
aaa=111
bbb=222
import java.io.*;
import java.util.Properties;
/**
* Properties類測試
* User: xiaohui
* Date: 2008-11-4 21:04:54
*/
public class TestProperties {
public static void main(String args[]) throws IOException {
testProperties();
test1();
}
public static void testProperties() throws IOException {
System.out.println("------------testProperties-------------");
//將properties文件加載到輸入字節流中
InputStream is = new FileInputStream("D:\\myprojects\\lession4\\src\\stu\\ttt.properties");
//創建一個Properties容器
Properties prop = new Properties();
//從流中加載properties文件信息
prop.load(is);
//循環輸出配置信息
for (Object key : prop.keySet()) {
System.out.println(key + "=" + prop.get(key));
}
//定義一個輸出流
OutputStream os1 = new FileOutputStream("C:\\ttt.xml");
OutputStream os2 = new FileOutputStream("C:\\ttt.properties");
//從Properties對象導出導出到xml
prop.storeToXML(os1, "我從properties導出的XML配置文件");
//從Properties對象導出properties文件
prop.store(os2, "我從properties導出的XML配置文件");
is.close();
os1.close();
os2.close();
//從xml加載配置信息,填充Properties容器
prop.loadFromXML(new FileInputStream("C:\\ttt.xml"));
//循環輸出配置信息
System.out.println("我從導出的xml加載配置文件信息!");
for (Object key : prop.keySet()) {
System.out.println(key + "=" + prop.get(key));
}
//修改Properties對象,並持久化到一個文件
prop.put("呵呵呵", "嘎嘎嘎");
OutputStream os3 = new FileOutputStream("C:\\ttt1.xml");
prop.storeToXML(os3, "我從properties導出的XML配置文件");
os3.close();
}
/**
* 以相對路徑方式加載properties文件
*
* @throws IOException
*/
public static void test1() throws IOException {
System.out.println("------------test1-------------");
Properties p = new Properties();
p.load(TestProperties.class.getResourceAsStream("/stu/ttt.properties"));
for (Object key : p.keySet()) {
System.out.println(key + "=" + p.get(key));
}
}
}
bbb=222
aaa=111
我從導出的xml加載配置文件信息!
bbb=222
aaa=111
------------test1-------------
bbb=222
aaa=111
Process finished with exit code 0
一、java.util.Properties類
引用部分摘自jdk 1.5 API幫助文檔。
Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。
一個屬性列表可包含另一個屬性列表作爲它的“默認值”;如果未能在原有的屬性列表中搜索到屬性鍵,則搜索第二個屬性列表。
因爲 Properties 繼承於 Hashtable,所以可對 Properties 對象應用 put 和 putAll 方法。但強烈反對使用這兩個方法,因爲它們允許調用方插入其鍵或值不是 Strings 的項。相反,應該使用 setProperty 方法。如果在“有危險”的 Properties 對象(即包含非 String 的鍵或值)上調用 store 或 save 方法,則該調用將失敗。
load 和 store 方法按簡單的面向行的格式加載和存儲屬性。此格式使用 ISO 8859-1 字符編碼。可以使用 Unicode 轉義符來編寫此編碼中無法直接表示的字符;轉義序列中只允許單個 'u' 字符。可使用 native2ascii 工具對屬性文件和其他字符編碼進行相互轉換。
loadFromXML(InputStream) 和 storeToXML(OutputStream, String, String) 方法按簡單的 XML 格式加載和存儲屬性。默認使用 UTF-8 字符編碼,但如果需要,可以指定某種特定的編碼。XML 屬性文檔具有以下 DOCTYPE 聲明:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
注意,導入或導出屬性時不訪問系統 URI (http://java.sun.com/dtd/properties.dtd);該系統 URI 僅作爲一個惟一標識 DTD 的字符串。
二、properties.dtd
properties.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>
通過Properties API讀寫的xml文檔應該遵循此DTD的定義。
三、常用方法
以下方法說明來自jdk 1.5 API幫助文檔,詳細解釋請查看JDK API幫助文檔。
- public String getProperty(String key):用指定的鍵在此屬性列表中搜索屬性。
- public String getProperty(String key, String defaultValue):用指定的鍵在屬性列表中搜索屬性。如果在屬性列表中未找到該鍵,則接着遞歸檢查默認屬性列表及其默認值。如果未找到屬性,則此方法返回默認值變量。
- public void setProperty(String key, String value):調用 Hashtable 的方法 put。
- public void load(InputStream inStream) throws IOException:從輸入流中讀取屬性列表(鍵和元素對)。
- public void loadFromXML(InputStream in)throws IOException, InvalidPropertiesFormatException :將指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。
- public void store(OutputStream out,String comments)throws IOException:將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。此方法適合使用 load 方法加載到 Properties 表中的格式。
- public void loadFromXML(InputStream in)throws IOException, InvalidPropertiesFormatException:將指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。
- public void storeToXML(OutputStream os,String comment)throws IOException:發出一個表示此表中包含的所有屬性的 XML 文檔。
使用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.classloader的getresourceasstream()方法
示例: 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.servletcontext的getresourceasstream()方法
示例:inputstream in = context.getresourceasstream(path)
properties p = new properties()
p.load(in)