`
liudaoru
  • 浏览: 1558889 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java获取properties配置文件

    博客分类:
  • java
阅读更多

根据网上的例子总结了一下。

其中cache.properties放到src下,也可以放到WEB-INF下。

 

package test.bwl;

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

public class Test {
	private static Properties properties = new Properties();

	public static void main(String[] args) {
		try {
			InputStream is = Test.class.getClassLoader().getResourceAsStream("cache.properties");
			properties.load(is);
			String size = properties.getProperty("cache.size");
			writeLog("配置成功!" + size);
		} catch (FileNotFoundException e) {
			writeLog("配置文件不存在!" + e.getMessage());
		} catch (IOException e) {
			writeLog("读取配置文件IO错误!" + e.getMessage());
		}
	}

	public static void writeLog(String strLog) {
		System.out.println(strLog);
	}
}

 

分享到:
评论
3 楼 AnotherApp 2011-12-28  
来学习了,多谢分享
2 楼 liudaoru 2008-12-09  
File path = new File(ConfigUtil.class.getClassLoader().getResource("/conf/").getPath());
1 楼 liudaoru 2008-11-09  
package test.bwl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

/**
 * 配置信息管理器
 * 
 * @author		bwl 
 * @version		1.0
 */
public class ConfigManager {

	/**
	 * 提供单例对象的静态内部类
	 */
	private static class SingletonHolder {
		public static ConfigManager instance = new ConfigManager();
	}

	/**
	 * 获取对象实例
	 * @return
	 */
	public static ConfigManager getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 存储问题列表的Map
	 */
	private Map<String, Properties> name2properties;

	/**
	 * 构造方法,请使用getInstance()获取实例
	 */
	private ConfigManager() {
		name2properties = Collections.synchronizedMap(new HashMap<String, Properties>());
		doInit();
	}

	/**
	 * 初始化方法 
	 */
	private void doInit() {
		try {
			File path = new File("./conf/");
			if (!path.exists()) {
				System.out.println("ConfilgManager Init Error: There is no folder named 'conf' under src file.");
				return;
			}
			File[] confFiles = path.listFiles(new DirFilter(".*\\.properties"));//\\
			for (int i = 0; i < confFiles.length; i++) {
				File f = confFiles[i];
				if (f.exists() && f.isFile()) {
					Properties properties = new Properties();
					InputStream is = new FileInputStream(f);
					properties.load(is);
					name2properties.put(f.getName(), properties);
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取配置项的值
	 * @param fileName	配置文件的名称
	 * @param key		关键码的值
	 * @return	配置项
	 */
	public String getProperty(String fileName, String key) {
		if (fileName == null || fileName.length() == 0) {
			return null;
		}
		Properties prop = name2properties.get(fileName);
		if (prop != null) {
			return prop.getProperty(key);
		}
		return null;
	}

	/**
	 * 获取整形的配置项的值
	 * @param fileName	配置文件的名称
	 * @param keyName	关键码的值
	 * @return	如果正确则返回数字,否则返回-1
	 */
	public int getIntProperty(String fileName, String key) {
		String value = this.getProperty(fileName, key);
		int result = -1;
		if (value == null) {
			return result;
		}
		try {
			result = Integer.parseInt(value);
			return result;
		} catch (Exception e) {
			//Do nothing
		}
		return result;
	}

	/**
	 * 过滤属性文件的内部类 
	 */
	class DirFilter implements FilenameFilter {

		/**
		 * 记录文件名格式的正则对象
		 */
		private Pattern pattern;

		public DirFilter(String regex) {
			pattern = Pattern.compile(regex);
		}

		public boolean accept(File dir, String name) {
			return pattern.matcher(new File(name).getName()).matches();
		}

	}

	public static void main(String[] args) {
		ConfigManager config = ConfigManager.getInstance();
		System.out.println(config.getIntProperty("cache.properties", "cache.size") + "");
		System.out.println(config.getProperty("javagroups.properties", "bus_name") + "");
	}

}

相关推荐

Global site tag (gtag.js) - Google Analytics