第三部分java應用程序設計類集屬性類properties_第1頁
第三部分java應用程序設計類集屬性類properties_第2頁
第三部分java應用程序設計類集屬性類properties_第3頁
已閱讀5頁,還剩5頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、JAVA 應用開發詳解第13章:Java類集 屬性類:PropertiesMLDN 軟件教學研發部本章目標掌握Properties類的使用可以向普通及XML格式的文件中保存及讀取屬性Properties類簡介 Properties類是一個專門操作屬性的類。此類是Hastable的子類。所有的屬性內容都是以字符串的形式保存的。Properties類定義:public class Properties extends HashtableProperties類的主要方法 No.方法類型描述1public Properties()構造構造一個空的屬性類2public Properties(Proper

2、ties defaults)常量構造一個指定屬性內容的屬性類3public String getProperty(String key)常量根據屬性的key取得屬性的value,如果沒有key則返回null4public String getProperty(String key, String defaultValue)普通根據屬性的key取得屬性的value,如果沒有key則返回defaultValue5public Object setProperty(String key, String value)普通設置屬性6public void list(PrintStream out)普通屬

3、性打印7public void load(InputStream inStream) throws IOException普通從輸入流中取出全部的屬性內容8public void loadFromXML(InputStream in) throws IOException,InvalidPropertiesFormatException普通從XML文件格式中讀取內容9public void store(OutputStream out,String comments) throws IOException普通將屬性內容通過輸出流輸出,同時聲明屬性的注釋10public void storeTo

4、XML(OutputStream os,String comment) throws IOException普通以XML文件格式輸出屬性,默認編碼11public void storeToXML(OutputStream os,String comment,String encoding) throws IOException普通以XML文件格式輸出屬性,用戶指定默認編碼實例操作一:設置和取得屬性 package pertiesdemo;import java.util.Properties;public class PropertiesDemo01 pub

5、lic 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.

6、out.println(3、SC屬性不存在,同時設置顯示的默認值:+ pro.getProperty(SC, 沒有發現);實例操作二:將屬性保存在普通文件之中 package pertiesdemo;import java.io.File;import java.io.FileOutputStream;import java.util.Properties;public class PropertiesDemo02 public static void main(String args) Properties pro = new Properties();

7、/ 創建Properties對象pro.setProperty(BJ, BeiJing);/ 設置內容pro.setProperty(TJ, TianJin);/ 設置內容pro.setProperty(NJ, NanJing);/ 設置內容/ 設置屬性文件的保存路徑File file = new File(D: + File.separator + perties);try / 保存屬性到普通文件之中,并設置注釋內容pro.store(new FileOutputStream(file), Area Info); catch (Exception e) e.printStac

8、kTrace();實例操作三:從普通文件之中讀取屬性內容 package pertiesdemo;import java.io.File;import java.io.FileInputStream;import java.util.Properties;public class PropertiesDemo03 public static void main(String args) Properties pro = new Properties(); / 創建Properties對象/ 設置屬性文件的操作路徑File file = new File(D:

9、 + File.separator + perties);try pro.load(new FileInputStream(file); / 讀取屬性文件 catch (Exception e) e.printStackTrace();System.out.println(BJ屬性值存在,內容是: + pro.getProperty(BJ);實例操作四:將屬性保存在XML文件之中 package pertiesdemo;import java.io.File;import java.io.FileOutputStream;import jav

10、a.util.Properties;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 + are

11、a.xml);try pro.storeToXML(new FileOutputStream(file), Area Info);/ 保存屬性到XML文件 catch (Exception e) e.printStackTrace();實例操作五:從XML文件之中讀取屬性 package pertiesdemo;import java.io.File;import java.io.FileInputStream;import java.util.Properties;public class PropertiesDemo05 public static void main(String args) Properties pro = new Properties(); / 創建Properties對象File file = new File(D: + File.separator

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論