Java屬性類:Properties的常用方法,javaproperties
Properties類本身是Hashtable類的子類,也是按照key-value的形式存放資料的.設定和取得屬性:
public class PropertiesDemo01{public static void main(String args[]){Properties pro = new Properties() ;// 建立Properties對象pro.setProperty("BJ","BeiJing") ;// 設定屬性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SC屬性不存在:" + pro.getProperty("SC")) ;System.out.println("3、SC屬性不存在,同時設定顯示的預設值:" + pro.getProperty("SC","沒有發現")) ;}};
將屬性儲存到普通的屬性檔案中:
public class PropertiesDemo02{public static void main(String args[]){Properties pro = new Properties() ;// 建立Properties對象pro.setProperty("BJ","BeiJing") ;// 設定屬性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;File file = new File("D:" + File.separator + "area.properteis") ;// 指定要操作的檔案try{pro.store(new FileOutputStream(file),"Area Info") ;// 儲存屬性到普通檔案}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}};
從屬性檔案中讀取內容:
public class PropertiesDemo03{public static void main(String args[]){Properties pro = new Properties() ;// 建立Properties對象File file = new File("D:" + File.separator + "area.properteis") ;// 指定要操作的檔案try{pro.load(new FileInputStream(file)) ;// 讀取屬性檔案}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;System.out.println("2、SH屬性存在:" + pro.getProperty("SH")) ;}};
將屬性儲存在XML檔案中:
public class PropertiesDemo04{public static void main(String args[]){Properties pro = new Properties() ;// 建立Properties對象pro.setProperty("BJ","BeiJing") ;// 設定屬性pro.setProperty("TJ","TianJin") ;pro.setProperty("NJ","NanJing") ;File file = new File("D:" + File.separator + "area.xml") ;// 指定要操作的檔案try{pro.storeToXML(new FileOutputStream(file),"Area Info") ;// 儲存屬性到普通檔案}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}}};
從XML檔案中讀取屬性:
public class PropertiesDemo05{public static void main(String args[]){Properties pro = new Properties() ;// 建立Properties對象File file = new File("D:" + File.separator + "area.xml") ;// 指定要操作的檔案try{pro.loadFromXML(new FileInputStream(file)) ;// 讀取屬性檔案}catch(FileNotFoundException e){e.printStackTrace() ;}catch(IOException e){e.printStackTrace() ;}System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;}};
java編程中Properties類的具體作用與使用
如果不熟悉 java.util.Properties類,那麼現在告訴您它是用來在一個檔案中儲存鍵-值對的,其中鍵和值是用等號分隔的。(如清單 1 所示)。最新動向的java.util.Properties 類現在提供了一種為程式裝載和儲存設定的更容易的方法: loadFromXML(InputStreamis) 和 storeToXML(OutputStream os, String comment) 方法。
一下是詳細的說明,希望能給大家帶來協助。
清單 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) >
......餘下全文>>
Java中屬性類及屬性檔案的定義分別是什
呵 、 我剛學到這 首先我要做的是串連資料庫(SQL2005) 把資料庫連接的驅動類 url 等 資訊放在 那個屬性檔案(*.db.properties)中 路徑是在src的根目錄下 為了方便讀取 下面是屬性檔案的資訊 你可以看看 driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=Books
user=sa
password= 其實定義屬性時 沒什麼難的 就是 名稱=值 的形式然後我通過一個靜態類載入屬性檔案的資訊 public final class Env extends Properties { //繼承Properties
private static Env instance; public static Env getInstance() {
if (instance != null) {
return instance;
} else {
makeInstance();
return instance;
}
}//這個是同步方法 private static synchronized void makeInstance() {
if (instance == null) {
instance = new Env();
}
}//載入屬性檔案
private Env(){
InputStream is=getClass().getResourceAsStream("/db.properties");
try{
load(is);
}catch(Exception e){
System.out.println("錯誤:沒有讀取屬性檔案");
}
}
}
這個靜態類用了單例模式 其實也不用這麼麻煩 關鍵是讀取屬性這段檔案 你看看 public static synchronized Connection getConnection()
throws Exception {
// 讀取配置資訊
String driverClassName = Env.getInstance().getProperty("driver");
String url = Env.getInstance().getProperty("url");
String user = Env.getInstance().getProperty("user");
String password = Env.getInstance().getProperty("password");
Connection con=null;
try{
//載入資料庫程式
Class.forName(driverClassName);
con=DriverManager.getConnection(url,user,password);
}catch(Exception e){
throw new Exception("不能取得資料庫連接!");
......餘下全文>>