Java中Properties的應用詳解。本站提示廣大學習愛好者:(Java中Properties的應用詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中Properties的應用詳解正文
Java中有個比擬主要的類Properties(Java.util.Properties),重要用於讀取Java的設置裝備擺設文件,各類說話都有本身所支 持的設置裝備擺設文件,設置裝備擺設文件中許多變量是常常轉變的,如許做也是為了便利用戶,讓用戶可以或許離開法式自己去修正相干的變量設置。明天,我們就開端Properties的應用。
Java中Properties的應用
Properties的文檔解釋:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
Properties類的描寫:
public class Properties extends Hashtable<Object,Object>
測試的項目構造以下:
1、在huhx.properties文件中,我們為也便利,參加一條數據:
name=huhx
2、將huhx.properties文件加載讀取,獲得響應的屬性
Properties properties = new Properties(); FileInputStream fis = new FileInputStream("huhx.properties"); properties.load(fis); System.out.println(properties.get("name"));
3、Properties的list辦法的應用
PrintStream printStream = System.out; properties.list(printStream);
list辦法的詳細代碼:
public void list(PrintStream out) { out.println("-- listing properties --"); Hashtable h = new Hashtable(); enumerate(h); for (Enumeration e = h.keys() ; e.hasMoreElements() ;) { String key = (String)e.nextElement(); String val = (String)h.get(key); if (val.length() > 40) { val = val.substring(0, 37) + "..."; } out.println(key + "=" + val); } }
4、Properties的store辦法的應用
OutputStream outputStream = new FileOutputStream("huhx.txt"); properties.store(outputStream, "comments");
5、Properties的storeToXML辦法的應用
OutputStream outputStream2 = new FileOutputStream("huhx.xml"); properties.storeToXML(outputStream2, "comments");
6、終究生成的文件以下:
huhx.txt:
#comments #Thu May 19 19:19:36 CST 2016 name=huhx
huhx.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>comments</comment> <entry key="name">huhx</entry> </properties>
友誼鏈接,PropertiesTest.java:
package com.huhx.linux; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.util.Properties; public class PropertiesTest { public static void main(String[] args) throws Exception { // 普通Properties的應用 Properties properties = new Properties(); FileInputStream fis = new FileInputStream("huhx.properties"); properties.load(fis); System.out.println(properties.get("name")); // 以下是測試的部門 PrintStream printStream = System.out; properties.list(printStream); OutputStream outputStream = new FileOutputStream("huhx.txt"); properties.store(outputStream, "comments"); OutputStream outputStream2 = new FileOutputStream("huhx.xml"); properties.storeToXML(outputStream2, "comments"); } }
以上所述是小編給年夜家引見的Java中Properties的應用詳解,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!