Java中Properties的使用詳解
Java中有個(gè)比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語(yǔ)言都有自己所支 持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。今天,我們就開(kāi)始Properties的使用。
Java中Properties的使用
Properties的文檔說(shuō)明:
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>
測(cè)試的項(xiàng)目結(jié)構(gòu)如下:

一、在huhx.properties文件中,我們?yōu)橐卜奖悖尤胍粭l數(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"));
// 以下是測(cè)試的部分
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的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java+swing實(shí)現(xiàn)經(jīng)典貪吃蛇游戲
貪吃蛇(也叫做貪食蛇)游戲是一款休閑益智類游戲,有PC和手機(jī)等多平臺(tái)版本。既簡(jiǎn)單又耐玩。本文將通過(guò)java的swing來(lái)實(shí)現(xiàn)這一游戲,需要的可以參考一下2022-01-01
SpringBoot發(fā)現(xiàn)最新版Druid重大問(wèn)題(坑)
這篇文章主要介紹了SpringBoot發(fā)現(xiàn)最新版Druid重大問(wèn)題(坑),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
spring boot微服務(wù)自定義starter原理詳解
這篇文章主要介紹了spring boot微服務(wù)自定義starter原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java調(diào)用通義千問(wèn)API的詳細(xì)完整步驟
通義千問(wèn)是阿里云自主研發(fā)的大語(yǔ)言模型,能夠在用戶自然語(yǔ)言輸入的基礎(chǔ)上,通過(guò)自然語(yǔ)言理解和語(yǔ)義分析,理解用戶意圖,在不同領(lǐng)域、任務(wù)內(nèi)為用戶提供服務(wù)和幫助,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用通義千問(wèn)API的詳細(xì)完整步驟,需要的朋友可以參考下2024-02-02
java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo
這篇文章主要為大家介紹了java實(shí)現(xiàn)用戶簽到BitMap功能實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Maven編譯Fatal?error?compiling:無(wú)效的目標(biāo)發(fā)行版:11問(wèn)題及解決
在Java11中編譯Springboot工程時(shí)遇到問(wèn)題,解決方法是在pom.xml文件中指定Maven的Java編譯器版本,可以使用MavenJava編譯器屬性或插件,在Java9及以后的版本中,也要使用插件并設(shè)置release屬性2024-12-12
Java最常用的6個(gè)簡(jiǎn)單的計(jì)算題
本篇文章給大家整理的在JAVA中最常用到的簡(jiǎn)單的計(jì)算題,對(duì)此有興趣的朋友可以測(cè)試參考下。2018-02-02

