Java中Properties的使用詳解
Java中有個比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語言都有自己所支 持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。今天,我們就開始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>
測試的項(xiàng)目結(jié)構(gòu)如下:
一、在huhx.properties文件中,我們?yōu)橐卜奖?,加入一條數(shù)據(jù):
name=huhx
二、將huhx.properties文件加載讀取,得到相應(yīng)的屬性
Properties properties = new Properties(); FileInputStream fis = new FileInputStream("huhx.properties"); properties.load(fis); System.out.println(properties.get("name"));
三、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); } }
四、Properties的store方法的使用
OutputStream outputStream = new FileOutputStream("huhx.txt"); properties.store(outputStream, "comments");
五、Properties的storeToXML方法的使用
OutputStream outputStream2 = new FileOutputStream("huhx.xml"); properties.storeToXML(outputStream2, "comments");
六、最終生成的文件如下:
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的使用詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解使用jquery.i18n.properties 實(shí)現(xiàn)web前端國際化
- 基于jQuery.i18n實(shí)現(xiàn)web前端的國際化
- java讀取properties配置文件的方法
- Java遍歷Properties所有元素的方法實(shí)例
- java獲取properties屬性文件示例
- Java讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法
- 詳解五種方式讓你在java中讀取properties文件內(nèi)容不再是難題
- java遍歷properties文件操作指南
- ajax讀取properties資源文件數(shù)據(jù)的方法
- Java中的幾種讀取properties配置文件的方式
- 詳解使用jQuery.i18n.properties實(shí)現(xiàn)js國際化
相關(guān)文章
Java利用剪貼板實(shí)現(xiàn)交換程序間數(shù)據(jù)的方法
這篇文章主要介紹了Java利用剪貼板實(shí)現(xiàn)交換程序間數(shù)據(jù)的方法,需要的朋友可以參考下2014-07-07SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例
這篇文章主要介紹了SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12