1. 程式人生 > >【Java程式設計】寫入、讀取、遍歷Properties檔案

【Java程式設計】寫入、讀取、遍歷Properties檔案

在Java開發中通常我們會儲存配置引數資訊到屬性檔案。這種屬性檔案能夠是擁有鍵值對的屬性檔案,也能夠是XML檔案。關於XML檔案的操作,請參考博文【Java程式設計】DOM XML Parser 解析、遍歷、建立XML

在該篇博文中,我將展示怎樣向屬性檔案寫入鍵值對。怎樣讀取屬性檔案裡的鍵值對,怎樣遍歷屬性檔案。

1、向屬性檔案裡寫入鍵值對


特別注意:

Properties類呼叫setProperty方法將鍵值對儲存到記憶體中。此時能夠通過getProperty方法讀取,propertyNames()方法進行遍歷,可是並沒有將鍵值對持久化到屬性檔案裡。故須要呼叫store()方法持久化鍵值對到屬性檔案裡

。這裡的store方法相似於Android SharedPreferences的commit()方法

package com.andieguo.propertiesdemo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

import junit.framework.TestCase;

public class PropertiesTester extends TestCase {

	public void writeProperties() {
		Properties properties = new Properties();
		OutputStream output = null;
		try {
			output = new FileOutputStream("config.properties");
			properties.setProperty("url", "jdbc:mysql://localhost:3306/");
			properties.setProperty("username", "root");
			properties.setProperty("password", "root");
			properties.setProperty("database", "bbs");//儲存鍵值對到記憶體
			properties.store(output, "andieguo modify" + new Date().toString());// 儲存鍵值對到檔案裡
		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
執行單元測試後。屬性檔案內容例如以下:


2、讀取屬性檔案裡的鍵值對


public class PropertiesTester extends TestCase {

	public void loadProperties() {
		Properties properties = new Properties();
		InputStream input = null;
		try {
			input = new FileInputStream("config.properties");//載入Java專案根路徑下的配置檔案
			properties.load(input);// 載入屬性檔案
			System.out.println("url:" + properties.getProperty("url"));
			System.out.println("username:" + properties.getProperty("username"));
			System.out.println("password:" + properties.getProperty("password"));
			System.out.println("database:" + properties.getProperty("database"));
		} catch (IOException io) {
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

執行單元測試方法。console輸出的output例如以下:


3、遍歷屬性檔案裡的鍵值對

package com.andieguo.propertiesdemo;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import junit.framework.TestCase;

public class PropertiesTester extends TestCase {

	public void printAll() {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			String filename = "config.properties";
			input = getClass().getClassLoader().getResourceAsStream(filename);
			if (input == null) {
				System.out.println("Sorry, unable to find " + filename);
				return;
			}
			prop.load(input);
			//方法一:
			Set<Object> keys = prop.keySet();//返回屬性key的集合
			for(Object key:keys){
				System.out.println("key:"+key.toString()+",value:"+prop.get(key));
			}
			//方法二:
			Set<Entry<Object, Object>> entrys =	prop.entrySet();//返回的屬性鍵值對實體
			for(Entry<Object, Object> entry:entrys){
				System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
			}
			//方法三:
			Enumeration<?

> e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println("Key:" + key + ",Value:" + value); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

4、其它方法

public void list(PrintStream out)

將屬性列表輸出到指定的輸出流。此方法對除錯非常實用。

public void storeToXML(OutputStream os,Stringcomment) throws IOException

發出一個表示此表中包括的全部屬性的 XML 文件。

5、參考

Java Properties File Examples(推薦)

Java Property File example with write, read, load from Classpath and property xml file

Java - The Properties Class

6、你可能感興趣的文章

【Java程式設計】DOM XML Parser解析、遍歷、建立XML

【Java程式設計】SAX XML Parser解析、生成XML檔案