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

首頁 > 學院 > 開發設計 > 正文

java中的Properties類的操作

2019-11-15 00:43:36
字體:
來源:轉載
供稿:網友
java中的PRoperties類的操作

java中有個比較重要的類:Properties類,該類主要用于讀取java的配置文件。每種語言都自己所支持的配置文件。java中的配置常為*.perperties文件,文件格式為文本格式,內容格式為“健=值”對格式,文本注釋信息可用#注釋。

Properties繼承Hashtable

它提供了幾個主要的方法:

1.getProperty ( String key),用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value。

2.load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。

3.setProperty ( String key, String value),調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。

4.store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。

5.clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。

二、Java讀取Properties文件

Java讀取Properties文件的方法有很多

但是最常用的還是通過java.lang.Class類的getResourceAsStream(String name)方法來實現,如下可以這樣調用:

InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫程序的,用此一種足夠。

或者下面這種也常用:

InputStream in = new BufferedInputStream(new FileInputStream(filepath));

三、相關實例

下面列舉幾個實例,加深對Properties類的理解和記憶。

我們知道,Java虛擬機(JVM)有自己的系統配置文件(system.properties),我們可以通過下面的方式來獲取。

1、獲取JVM的系統屬性

public class Property {    public static void main(String args[]){        System.out.println(new Date());        Properties p=System.getProperties();        p.list(System.out);  //print all properties about JVM    }}

2.讀取本地配置文件test.properties并輸出

public class Property {    public static void main(String args[]){//        System.out.println(new Date());//        Properties p=System.getProperties();//        p.list(System.out);  //print all properties about JVM                Properties text=new Properties();        try {            text.load(new FileInputStream("E://workspace//ch01//text.properties"));        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }                @SuppressWarnings("rawtypes")    /*    Enumeration接口也在java.util包中 它的功能與Iterator差不多 都是用來遍歷集合中的元素         但是枚舉Enumeration只提供了遍歷Vector和Hashtable類型集合元素的功能*/        Enumeration num=text.propertyNames();  //獲取配件文件內容中的關于key的Enumeration                while(num.hasMoreElements()){            String key=(String)num.nextElement();            String value=text.getProperty(key);            System.out.println(key + "=" + value);        }    }}

結果:

age=23name=linyuhuanweight=140long=212

3、一個比較綜合的實例

根據key讀取value

讀取properties的全部信息

寫入新的properties信息

package ch01;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;/** * 關于Properties類的常用操作 * @author lin * 2015年6月30日 */public class TestProperties {    public static void main(String[] args) throws IOException{            WriteProperties("E://workspace//ch01//text.properties", "add2","2121");    }        //根據key讀取value    public static String GetValueByKey(String filePath,String key){        Properties pps = new Properties();        try{            InputStream in= new BufferedInputStream(new FileInputStream(filePath));            pps.load(in);            String value = pps.getProperty(key);            System.out.println(key + "=" + value);            return value;        }catch(IOException e){            e.printStackTrace();            return null;        }    }        //讀取配置文件的全部信息    @SuppressWarnings("rawtypes")    public static void GetAllProperties(String filePath) throws IOException{        Properties pps = new Properties();        InputStream in = new BufferedInputStream(new FileInputStream(filePath));        pps.load(in);        Enumeration num = pps.propertyNames(); //獲取配置文件中的所有屬性名enumeration        while(num.hasMoreElements()){            String key= (String)num.nextElement();            String value=pps.getProperty(key);            System.out.println(key + "=" + value);        }    }        //寫入Properties信息    public static void WriteProperties(String filePath,String pKey,String pValue) throws IOException {        Properties pps = new Properties();        InputStream in= new FileInputStream(filePath);        pps.load(in);        OutputStream out = new FileOutputStream(filePath);        pps.setProperty(pKey, pValue);        pps.store(out, "Update " + pKey + " name");    }}

結果:test.properties文件

#Update add2 name#Tue Jun 30 17:07:55 CST 2015age=23name=linyuhuanweight=140add2=2121long=212

參考鏈接:http://www.survivalescaperooms.com/bakari/p/3562244.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武清区| 黔西县| 桓台县| 平泉县| 南充市| 南部县| 利川市| 武城县| 西藏| 开封县| 南江县| 苍南县| 德兴市| 顺平县| 浦江县| 聊城市| 盱眙县| 革吉县| 邵东县| 凭祥市| 杂多县| 通渭县| 琼中| 林西县| 西乌珠穆沁旗| 濉溪县| 江源县| 泰宁县| 化州市| 南平市| 嘉禾县| 壤塘县| 吉木乃县| 凤庆县| 沙坪坝区| 札达县| 金山区| 大兴区| 井冈山市| 石城县| 富源县|