国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中Properties的使用詳解

2019-11-26 14:18:54
字體:
來源:轉載
供稿:網友

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>

測試的項目結構如下:

一、在huhx.properties文件中,我們為也方便,加入一條數據:

name=huhx

二、將huhx.properties文件加載讀取,得到相應的屬性

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 2016name=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的使用詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瓦房店市| 牙克石市| 太康县| 项城市| 嘉禾县| 喀喇沁旗| 务川| 合作市| 固原市| 河西区| 巴彦县| 肇庆市| 香格里拉县| 宁化县| 张家港市| 崇仁县| 大安市| 海淀区| 云安县| 正蓝旗| 南江县| 嵊州市| 成都市| 耿马| 林口县| 左贡县| 彩票| 青海省| 都兰县| 邯郸县| 南宫市| 绥阳县| 高要市| 青冈县| 新竹市| 普宁市| 威信县| 平山县| 平山县| 平昌县| 齐齐哈尔市|