Java读取.properties文件

config.properties文件内容如下:

userName=\u5F20\u4E09
password=19920720

java代码如下:

package com.pifeng.properties;

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

import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * @function java读取config.properties文件
 * @author 皮锋
 * @date 2016-1-16
 */
public class PropertiesUtils {

	public static String userName = null;
	public static String password = null;

	/**
	 * @date 2016/8/11
	 * @author 皮锋
	 * @fun 使用spring提供的工具类读取.properties文件
	 */
	private static void springPropUtilReadProp() {
		try {
			Properties props=PropertiesLoaderUtils.loadAllProperties("config.properties");
			userName = props.getProperty("userName");
			password = props.getProperty("password");
			props.setProperty("function", "springPropUtilReadProp()");// 往属性文件插值
			System.out.println(userName);
			System.out.println(password);
			System.out.println("function:"+props.getProperty("function"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @date 2016/8/11
	 * @author 皮锋
	 * @fun 使用Properties类读写Properties属性文件,用IO流的方式
	 */
	private static void streamReadProp() {
		Properties properties = new Properties();// 属性集合对象
		InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");// 获取路径并转换成流
		try {
			properties.load(path);// 将属性文件流装载到Properties对象中
			userName = properties.getProperty("userName");
			password = properties.getProperty("password");
			properties.setProperty("function", "streamReadProp()");// 往属性文件插值
			System.out.println(userName);
			System.out.println(password);
			System.out.println("function:"+properties.getProperty("function"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @date 2016/8/11
	 * @author 皮锋
	 * @fun 读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息)
	 */
	private static void resourceBundleReadProp() {
		ResourceBundle resourceBundle = ResourceBundle.getBundle("config");
		userName = resourceBundle.getString("userName");
		password = resourceBundle.getString("password");
		System.out.println(userName);
		System.out.println(password);
	}

	public static void main(String[] args) {
		//1.读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息)
		resourceBundleReadProp();
		//2.使用Properties类读写Properties属性文件,用IO流的方式
		streamReadProp();
		//3.使用spring提供的工具类读取.properties文件
		springPropUtilReadProp();
	}
}

运行结果:

张三
19920720
张三
19920720
function:streamReadProp()
张三
19920720
function:springPropUtilReadProp()

 

新版本:

package com.transfar.base.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;

import com.transfar.base.factory.LogFactory;

/**
 * @func java读取.properties文件
 * @author 皮锋
 * @date 2018/01/04
 */
public class PropertiesUtils {

	/**
	 * @date 2018/01/04
	 * @author 皮锋
	 * @func 读取资源属性文件(properties),无缓存方式
	 */
	public static String readPropertyNoCache(String filePath, String param) {
		try {
			String url = PropertiesUtils.class.getResource("/").getPath() + filePath;
			Properties prop = new Properties();
			InputStream in = new BufferedInputStream(new FileInputStream(url));
			// 将属性文件流装载到Properties对象中
			// prop.load(in);
			prop.load(new InputStreamReader(in, "utf-8"));
			return prop.getProperty(param);
		} catch (IOException e) {
			LogFactory.getLogger().error("读properties属性文件异常!", e);
		}
		return null;
	}

	/**
	 * @date 2016/08/11
	 * @author 皮锋
	 * @func 读取资源属性文件(properties),用IO流的方式
	 */
	public static String readProperty(String filePath, String param) {
		// 属性集合对象
		Properties properties = new Properties();
		// 获取路径并转换成流
		InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
		try {
			// 将属性文件流装载到Properties对象中
			properties.load(path);
			return properties.getProperty(param);
		} catch (IOException e) {
			LogFactory.getLogger().error("读properties属性文件异常!", e);
		}
		return null;
	}

	/**
	 * @date 2016/08/11
	 * @author 皮锋
	 * @func 读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息)
	 */
	public static String getProperty(String filePath, String param) {
		ResourceBundle resourceBundle = ResourceBundle.getBundle(filePath);
		return resourceBundle.getString(param);
	}

	/**
	 * 
	 * @description 读取.properties配置文件的内容至Map中
	 * @param propertiesFile
	 * @param param
	 * @return map
	 */
	public static Map<String, String> read2Map(String propertiesFile, String param) {
		ResourceBundle rb = ResourceBundle.getBundle(propertiesFile);
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> enu = rb.getKeys();
		while (enu.hasMoreElements()) {
			String obj = enu.nextElement();
			// 传了参数
			if (StringUtils.isNotEmpty(param)) {
				if (obj.indexOf(param) != -1) {
					String objv = rb.getString(obj);
					map.put(obj, objv);
				}
			}
			// 没传参数
			else {
				String objv = rb.getString(obj);
				map.put(obj, objv);
			}
		}
		return map;
	}

	/**
	 * @func 写properties文件
	 * @param filePath
	 * @param pKey
	 * @param pValue
	 */
	public static boolean writeProperties(String filePath, String pKey, String pValue) {
		try {
			String url = PropertiesUtils.class.getResource("/").getPath() + filePath;
			Properties prop = new Properties();
			InputStream in = new BufferedInputStream(new FileInputStream(url));
			// 将属性文件流装载到Properties对象中
			prop.load(in);
			// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
			// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
			OutputStream out = new FileOutputStream(url);
			prop.setProperty(pKey, pValue);
			// 以适合使用 load 方法加载到 Properties 表中的格式,
			// 将此 Properties 表中的属性列表(键和元素对)写入输出流
			prop.store(out, "Update " + pKey + " name");
			return true;
		} catch (Exception e) {
			LogFactory.getLogger().error("写properties属性文件异常!", e);
			return false;
		}
	}

	public static void main(String[] args) {
		// 1.读取资源属性文件(properties),然后根据.properties文件的名称信息(本地化信息)
		String result1 = getProperty("conf/db", "jdbc_url");
		System.out.println(result1);
		// 2.使用Properties类读Properties属性文件,用IO流的方式
		String result2 = readProperty("conf/db.properties", "jdbc_url");
		System.out.println(result2);

		// 写Properties文件
		writeProperties("conf/db.properties", "jdbc_url", "jdbc:mysql://localhost:3306/test");

		// 3.使用无缓存的方式读取Properties属性文件
		String result3 = readPropertyNoCache("conf/db.properties", "jdbc_url");
		System.out.println(result3);
	}
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

E%3Dmc%B2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值