自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java中Properties的使用詳解

開(kāi)發(fā) 后端
Java中有個(gè)比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語(yǔ)言都有自己所支 持的配置文件,配置文件中很多變量是經(jīng)常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關(guān)的變量設(shè)置。

[[171639]]

這篇文章主要介紹了Java中Properties的使用詳解的相關(guān)資料,需要的朋友可以參考下。

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

測(cè)試的項(xiàng)目結(jié)構(gòu)如下:

 

一、在huhx.properties文件中,我們?yōu)橐卜奖?,加入一條數(shù)據(jù):

  1. name=huhx  

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

  1. Properties properties = new Properties(); 
  2. FileInputStream fis = new FileInputStream("huhx.properties"); 
  3. properties.load(fis); 
  4. System.out.println(properties.get("name"));  

三、Properties的list方法的使用

  1. PrintStream printStream = System.out; 
  2. properties.list(printStream);  

list方法的具體代碼:

  1. public void list(PrintStream out) { 
  2. out.println("-- listing properties --"); 
  3. Hashtable h = new Hashtable(); 
  4. enumerate(h); 
  5. for (Enumeration e = h.keys() ; e.hasMoreElements() ;) { 
  6. String key = (String)e.nextElement(); 
  7. String val = (String)h.get(key); 
  8. if (val.length() > 40) { 
  9. valval = val.substring(0, 37) + "..."; 
  10. out.println(key + "=" + val); 

四、Properties的store方法的使用

  1. OutputStream outputStream = new FileOutputStream("huhx.txt"); 
  2. properties.store(outputStream, "comments");  

 

五、Properties的storeToXML方法的使用

  1. OutputStream outputStream2 = new FileOutputStream("huhx.xml"); 
  2. properties.storeToXML(outputStream2, "comments");  
 

六、最終生成的文件如下:

  1. huhx.txt: 
  2. #comments 
  3. #Thu May 19 19:19:36 CST 2016 
  4. name=huhx  
  5. huhx.xml: 
  6. <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
  7. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 
  8. <properties> 
  9. <comment>comments</comment> 
  10. <entry key="name">huhx</entry> 
  11. </properties>  

 友情鏈接,PropertiesTest.java:

  1. package com.huhx.linux; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream; 
  4. import java.io.OutputStream; 
  5. import java.io.PrintStream; 
  6. import java.util.Properties; 
  7. public class PropertiesTest { 
  8. public static void main(String[] args) throws Exception { 
 

// 一般Properties的使用

  1. Properties properties = new Properties(); 
  2. FileInputStream fis = new FileInputStream("huhx.properties"); 
  3. properties.load(fis); 
  4. System.out.println(properties.get("name")); 
 

// 以下是測(cè)試的部分

  1. PrintStream printStream = System.out; 
  2. properties.list(printStream); 
  3. OutputStream outputStream = new FileOutputStream("huhx.txt"); 
  4. properties.store(outputStream, "comments"); 
  5. OutputStream outputStream2 = new FileOutputStream("huhx.xml"); 
  6. properties.storeToXML(outputStream2, "comments"); 

以上所述是小編給大家介紹的Java中Properties的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

責(zé)任編輯:趙寧寧 來(lái)源: 腳本之家
相關(guān)推薦

2021-04-13 09:20:21

JavaUnsafejava8

2009-09-27 10:28:12

Hibernate.p

2011-03-28 09:35:06

iBaitsSqlMapClien

2009-07-20 14:24:13

Math.pow()方Java ME

2025-01-15 07:00:00

Java代碼Lambda

2009-06-05 10:35:02

struts.prop配置文件

2010-09-08 17:15:45

SQL循環(huán)結(jié)構(gòu)

2020-09-26 07:19:46

Java

2010-03-19 16:07:41

Exchange 20

2011-05-13 09:16:03

Mysqlmysqldump命令

2011-07-20 09:16:02

MongoDB索引稀疏索引

2009-08-17 08:42:48

LinuxScreen命令使用技巧

2015-09-09 08:45:49

JavaThreadLocal

2009-07-16 13:51:43

2011-08-15 14:27:51

CocoaRunLoop

2009-06-05 10:52:45

struts2深入詳解配置文件

2009-06-30 15:18:10

StringBuildJava

2025-02-05 12:22:21

2022-06-26 08:39:19

Spring容器字段格式化

2009-10-29 09:15:32

ASP.NET MVCDropDownLis
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)