Java读取配置文件 xxx.properties 类似这样的文件

在实际开发中, 为了项目配置的灵活, 参数设置一般都会放到配置文件中, 比如:

log4j.properties  jdbc.properties 等等

这些配置文件一般情况下, 使用框架, 交给spring加载, 但是如果让我们自己读取配置信息, 该怎么办?

有一些时候, 我们需要自己根据业务需求, 来读取不同的配置文件信息,

下面案例, 两个方法都可以, 一个静态, 一个动态, 根据需要使用:

package utils;

import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author wxy e-mail:wxypersonal@163.com
 * @date 创建时间:2017年5月18日 上午9:28:00
 * @version 1.0
 * @company: xxx科技公司
 * @description: 读取配置文件
 * */
public class ReadConfigUtils {
	
	private static Logger logger= LoggerFactory.getLogger(ReadConfigUtils.class);

	/**
	 * @param resourceName 要读取的资源文件名称: 如:jdbc.properties ps:文件的后缀名properties不能传进去,否则报错
	 * @param property 要读取的资源文件中的某一个属性名: 如url, username, password.....
	 * @return 返回属性值
	 */
	public static String getDataFromPropertiseFile(String resourceName, String property) {
		
		if (resourceName == null || "".equals(resourceName)) {
			logger.debug("getDataFromPropertiseFile resourceName is null, 读取的资源文件不存在");
			return null;
		}
		if (property == null || "".equals(property)) {
			logger.debug("getDataFromPropertiseFile property is null 资源文件中, 没有这个属性值");
			return null;
		}
		ResourceBundle resource = null;
		try {
			resource = ResourceBundle.getBundle(resourceName);
			if (resource == null) {
				logger.debug(resourceName + "配置文件不存在");
				return null;
			}
			return resource.getString(property);
		} catch (Exception e) {
			logger.error(resourceName + "配置文件不存在", e);
			return null;
		}
	}

	

	/**
	 * 这是一个读取.properties配置文件的类, 如jdbc.properties. 
	 * @param str 读取的配置文件路径, 默认路径是classpath下
	 * 
	 * @return 返回读取好的配置文件信息 这个返回值可以用.getProperty("属性名") 拿到相对应的属性值.
	 */
	public Properties readConfig(String str) {

		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream(str));
		} catch (NullPointerException e2) {
			e2.printStackTrace();
			System.out.println("空指针异常, 找不到这个配置文件");
			return null;
		} catch (IOException e3) {
			e3.printStackTrace();
			System.out.println("流读取异常");
			return null;
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("读取配置文件信息: " + str + " 失败");
			return null;
		}
		return prop;
	}

	/**
	 * 验证方法
	 * 
	 * @throws Exception
	 */
	public static void main(String[] args) {
		ReadConfigUtils r = new ReadConfigUtils();
		Properties config = r.readConfig("wxy/demo/jdbc2.properties");
		// Properties config = r.readConfig("jdbc.properties");
		System.out.println("123");
		System.out.println(config.getProperty("name"));
		System.out.println(config.getProperty("password"));
		System.out.println(config.getProperty("wxy"));
		
		
		System.out.println("....................分割线............................");
		String value = ReadConfigUtils.getDataFromPropertiseFile("wxy/demo/jdbc2", "name");
		System.out.println(value);
	}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class ReadConfigation{ /** * 属性文件全名 */ private static final String PFILE ="Config.properties"; /** * 对应于属性文件文件对象变量 */ private File m_file = null; /** * 属性文件的最后修改日期 */ private long m_lastModifiedTime = 0; /** * 属性文件所对应的属性对象变量 */ private Properties m_props = null; /** * 本类可能存在的惟一的一个实例 */ private static ReadConfigation m_instance =new ReadConfigation(); /** * 私有的构造子,用以保证外界无法直接实例化 */ private ReadConfigation() { m_file = new File(PFILE); m_lastModifiedTime = m_file.lastModified(); if(m_lastModifiedTime == 0){ System.err.println(PFILE +" file does not exist!"); } m_props = new Properties(); try { m_props.load(new FileInputStream(PFILE)); } catch(Exception e) { e.printStackTrace(); } } /** * 静态工厂方法 * @return 返还ReadConfigation 类的单一实例 */ synchronized public static ReadConfigation getInstance() { return m_instance; } /** * 读取一特定的属性项 * * @param name 属性项的项名 * @param defaultVal 属性项的默认值 * @return 属性项的值(如此项存在), 默认值(如此项不存在) */ public String getConfigItem(String name,String defaultVal) { long newTime = m_file.lastModified(); // 检查属性文件是否被其他程序 // 如果是,重新读取文件 if(newTime == 0) { // 属性文件不存在 if(m_lastModifiedTime == 0){ System.err.println(PFILE+ " file does not exist!"); }else{ System.err.println(PFILE+ " file was deleted!!"); } return defaultVal; }else if(newTime > m_lastModifiedTime){ // Get rid of the old properties m_props.clear(); try { m_props.load(new FileInputStream(PFILE)); }catch(Exception e){ e.printStackTrace(); } } m_lastModifiedTime = newTime; String val = m_props.getProperty(name); if( val == null ) { return defaultVal; } else { return val; } } /** * 读取一特定的属性项 * * @param name 属性项的项名 * @return 属性项的值(如此项存在), 空(如此项不存在) */ public String getConfigItem(String name){ return getConfigItem(name,""); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值