properties操作。

1.讀取properties

假設properties文件在項目跟目錄/classes/path.properties

String rootPath=this.getClass().getClassLoader().getResource("/").getPath()+"path.properties";

Properties pps = new Properties();
		InputStream in = new BufferedInputStream(new FileInputStream(rootPath));
		pps.load(in);

2.遍歷

/**
 * 
 */
package pkg;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * @author qefee
 * 
 */
public class ShowProperties {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Properties properties = System.getProperties();

		// show keys
		showKeys(properties);

		// show values
		showValues(properties);

		// show keys and values
		showKeysAndValues(properties);
	}

	/**
	 * @param properties
	 */
	private static void showKeys(Properties properties) {
		Enumeration<?> enu = properties.propertyNames();
		while (enu.hasMoreElements()) {
			Object key = enu.nextElement();
			System.out.println(key);
		}
	}

	/**
	 * @param properties
	 */
	private static void showValues(Properties properties) {
		Enumeration<Object> enu = properties.elements();
		while (enu.hasMoreElements()) {
			Object value = enu.nextElement();
			System.out.println(value);
		}
	}

	/**
	 * @param properties
	 */
	private static void showKeysAndValues(Properties properties) {
		Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			System.out.println("key   :" + key);
			System.out.println("value :" + value);
			System.out.println("---------------");
		}
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章