【转】Java遍历Properties

/**
 * 
 */
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("---------------");
		}
	}

}

 

你可能感兴趣的:(properties)