屬性是程序中經常出現的形式。
在類集中提供了一種專門的Properties類。
public class Propertiesextends Hashtable<Object,Object>
Properties是HashTable子類,那么肯定也是Map子類。可以使用Map的全部操作。
但是一般情況下是單獨使用的。
設置和取得屬性
設置屬性。
Object setProperty(String key, String value) 調用 Hashtable 的方法 put。
得到屬性:
找到了返回值,沒找到返回null。
String getProperty(String key) 用指定的鍵在此屬性列表中搜索屬性。
和 找到了返回值,沒找到返回defaultValue默認值。
String getProperty(String key, String defaultValue) 用指定的鍵在屬性列表中搜索屬性。
驗證以上操作方法:
package 類集; import java.util.Properties; public class test1{ 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","沒有發現")) ; //沒找到返回默認值 } };
結果:
1、BJ屬性存在:BeiJing 2、SC屬性不存在:null 3、SC屬性不存在,同時設置顯示的默認值:沒有發現
將以上屬性寫入普通文件中,字節流操作。
void store(OutputStream out, String comments) 以適合使用 load(InputStream) 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。
代碼:
package 類集; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class test1{ 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() ; } } };
結果:

要注意要有文件的異常處理。
既然可以保存到普通文件中,那么可以讀取文件。
void load(InputStream inStream) 從輸入流中讀取屬性列表(鍵和元素對)。
讀取文件中屬性內容的代碼:
package 類集; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class test1{ 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")) ; } };
輸出結果:
1、BJ屬性存在:BeiJing 2、SH屬性存在:null
保存到XML文件中。
void storeToXML(OutputStream os, String comment) 發出一個表示此表中包含的所有屬性的 XML 文檔。
代碼:
package 類集; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class test1{ 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文件里的屬性
void loadFromXML(InputStream in) 將指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。
代碼:
package 類集; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class test1{ 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")) ; } };
操作結果:
1、BJ屬性存在:BeiJing
總結:
1,要想進一步了解屬性,可以學習后續的反射機制。了解屬性應用
2,屬性里的類型肯定都是字符串。因為操作最方便。
3,屬性可以向普通文件或者XML文件中保存或者讀取,按照指定格式向文件中任意擴充屬性。
