1. 程式人生 > >讀取多個Properties檔案

讀取多個Properties檔案

package net.hxtek.util;

import java.io.IOException;
/**
 * 讀取Properties檔案列舉類
 * 2014-3-13 下午2:12:18
 *
 */
public enum PropUtil {
	SMS("SMS", "sms.properties");
	private String key;
	private String path;
	private Properties props;

	PropUtil(String key, String path) {
		this.key = key;
		this.path = path;
	}

	public Properties getProp() {
		if (props == null) {
			initProps();
		}
		return props;
	}

	public String getKey() {
		return key;
	}
	
	public String getPath() {
		return path;
	}
	
	private synchronized void initProps() {
		InputStream in = null;
		props=new Properties();
		try {
			props.load(PropFile.class.getClassLoader().getResourceAsStream("/" + path));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}