propertysreader.java

来自「wmoa办公自动化系统 小型的JAVA项目 功能包括审批流 消息发布等」· Java 代码 · 共 71 行

JAVA
71
字号
package com.dudu.util;

import java.io.InputStream;
import java.util.Properties;

/**
 * <p>本类用提供一个线程同步的方法,读取资源文件中的配置信息</p>
 * 
 * @author siyn
 * @date 2008-7-10
 */
public class PropertysReader {
	private String file;

	private Properties properties;

	/**
	 * 构造 PropertysReader
	 * 
	 * @param {String} path 相对于classes的文件路径
	 */
	public PropertysReader(String path) {
		this.file = path;
		this.properties = new Properties();
	}

	/**
	 * <p>本方法根据资源名获取资源内容<p>
	 * 
	 * @param {String} key 资源文件内key
	 * @param {Stirng} defaultValue 默认值
	 * 
	 * @reaurn String key对应的资源内容
	 */
	public synchronized String getProperty(String key, String defaultValue) {
		try {
			InputStream in = this.getClass().getClassLoader().getResourceAsStream(this.file);

			properties.load(in);

		} catch (Exception ex1) {
			System.out.println("没有找到资源文件:" + this.file);
		}
		return properties.getProperty(key, defaultValue);
	}

	/** */
	/**
	 * <p>本方法根据资源名获取资源内容<p>
	 * 
	 * @param {String} key 资源文件内key
	 * @param {Stirng} defaultValue 默认值
	 * @param {boolean} isnull 如果配置文件value为空,是否使用默认值
	 * 
	 * @reaurn String key对应的资源内容
	 */
	public synchronized String getProperty(String key, String defaultValue, boolean isnull) {
		String value = null;
		value = getProperty(key, defaultValue);
		if (isnull && value == null && "".equals(value.trim()))
			value = defaultValue;
		return value;
	}

	public static void main(String[] args) {
		PropertysReader preader = new PropertysReader("log4j.properties");
		String rootLogger = preader.getProperty("log4j.appender.logfile", "defaul");
		System.out.println(rootLogger);
	}
}

⌨️ 快捷键说明

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