⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propsutil.java

📁 短信发送
💻 JAVA
字号:
/**
 * Created at Nov 21, 2008
 */
package com.jdev.util;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.jdev.util.jdom.XMLProperties;

/**
 * <p>Title: PropsUtil</p>
 * <p>Description: </p>
 * @author Lawrence
 * @version 1.0
 */
public class PropsUtil {

	public final static String module = PropsUtil.class.getName();

	public final static String XML_CONFIG_FILE = "config.xml";

	public final static String LOG = "log.level";
	public final static String LOG4J = "log.log4j";

	public final static String SETUPNAME = "setup";
	public final static String SETUPVALUE = "true";

	public static String ENCODING = "UTF-8";

	private Map<String, XMLProperties> propMap = new HashMap<String, XMLProperties>();

	private final static PropsUtil propsUtil = new PropsUtil();

	public static PropsUtil getInstance() {
		return propsUtil;
	}

	/**
	 * load default config file
	 */
	private PropsUtil() {
		loadProperties(XML_CONFIG_FILE);
	}

	public void loadProperties(String configName) {

		String pathCongfgName = getConfFile(configName);
		if (pathCongfgName != null) {
			XMLProperties properties = new XMLProperties(pathCongfgName);
			propMap.put(configName, properties);
			return;
		}

		InputStream stream = getConfStream(configName);
		if (stream != null) {
			XMLProperties properties = new XMLProperties(stream);
			propMap.put(configName, properties);
			return;
		}

		System.err.println(" cann't load config file:-->" + configName);

	}

	public String getConfFile(String file) {

		URL confURL = getClass().getClassLoader().getResource(file);
		if (confURL == null)
			confURL = getClass().getClassLoader().getResource(
					"META-INF/" + file);

		if (confURL == null)
			confURL = Thread.currentThread().getContextClassLoader()
					.getResource(file);
		if (confURL == null) {
			System.err.println(" cann't find config file:-->" + file);
		} else {
			File file1 = new File(confURL.getFile());
			if (file1.isFile())
				return confURL.getFile();
		}

		return null;

	}

	public InputStream getConfStream(String file) {

		InputStream stream = getClass().getClassLoader().getResourceAsStream(
				file);
		if (stream == null)
			stream = getClass().getClassLoader().getResourceAsStream(
					"META-INF/" + file);
		if (stream == null)
			stream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(file);
		if (stream == null) {
			System.err.println(" cann't find config file:-->" + file);
		}
		return stream;

	}

	public String getProperty(String name) {
		return getProperty(XML_CONFIG_FILE, name);
	}

	public String getProperty(String configName, String name) {
		XMLProperties properties = (XMLProperties) propMap.get(configName);
		String res = properties.getProperty(name);
		if (res == null)
			res = "";
		return res;
	}

	public void setProperty(String name, String value) {
		setProperty(XML_CONFIG_FILE, name, value);
	}

	public void setProperty(String configName, String name, String value) {
		XMLProperties properties = (XMLProperties) propMap.get(configName);
		properties.setProperty(name, value);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -