ResourceBundle与Properties读取配置文件

ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取。

ResourceBundle的单例使用如下

import java.util.ResourceBundle;

/**
 * 访问配置文件 - 单例
 * @author: cyq    
 * @since : 2015-09-30 10:17
 */

public class Configuration {
	
	private static ResourceBundle rb    			= null;
	private volatile static Configuration instance	= null;
	
	private Configuration(String configFile) {
		rb = ResourceBundle.getBundle(configFile);
	}
	
	public static Configuration getInstance(String configFile) {
		if(instance == null) {
			synchronized(Configuration.class) {
				if(instance == null) {
					instance = new Configuration(configFile);
				}
			}
		}
		return instance;
	}
	
	public String getValue(String key) {
		return (rb.getString(key));
	}
}

Properties的使用

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesMain {
	/**
	 * 根据Key读取Value
	 *
	 * @param filePath
	 * @param key
	 * @return
	 */
	public static String getValueByKey(String filePath, String key) {
		Properties pps = new Properties();
		InputStream in = null;
		try {
			// in = new BufferedInputStream(new FileInputStream(filePath));
			in = PropertiesMain.class.getResourceAsStream(filePath);
			pps.load(in);
			String value = pps.getProperty(key);
			System.out.println(key + " = " + value);
			return value;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 读取Properties的全部信息
	 *
	 * @param filePath
	 * @throws IOException
	 */
	public static void getAllProperties(String filePath) throws IOException {
		Properties pps = new Properties();
		InputStream in = PropertiesMain.class.getResourceAsStream(filePath);
		pps.load(in);
		Enumeration en = pps.propertyNames(); // 得到配置文件的名字

		while (en.hasMoreElements()) {
			String strKey = (String) en.nextElement();
			String strValue = pps.getProperty(strKey);
			System.out.println(strKey + "=" + strValue);
		}
	}

	/**
	 * 写入Properties信息
	 *
	 * @param filePath
	 * @param pKey
	 * @param pValue
	 * @throws IOException
	 */
	public static void writeProperties(String filePath, String pKey,
			String pValue) throws IOException {
		Properties pps = new Properties();

		InputStream in = new FileInputStream(filePath);
		// 从输入流中读取属性列表(键和元素对)
		pps.load(in);
		OutputStream out = new FileOutputStream(filePath);
		Object setProperty = pps.setProperty(pKey, pValue);
		System.out.println(setProperty);
		// 将此 Properties 表中的属性列表(键和元素对)写入输出流
		pps.store(out, "Update " + pKey + " name");
	}

	public static void main(String[] args) throws IOException {
		// 打印当前目录
		// System.out.println(new File(".").getAbsolutePath());
		// 如果使用FileInputStream方式读取文件流 ./config/myconfig.properties
		// 如果使用getResourceAsStream方式读取文件流 /myconfig.properties
		// String value = getValueByKey("/myconfig.properties", "say.hello");
		// System.out.println(value);
		// getAllProperties("/myconfig.properties");
		// writeProperties("./config/myconfig.properties","long", "2112");
	}

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值