property文件的操作

1.读取property文件:

public static void main(String[] args) throws IOException {
		// 第一中方法
		Properties pro = PropertiesLoaderUtils
				.loadAllProperties("property/test.properties");
		System.out.println(pro.getProperty("name"));

		// 第2中方法
		Properties prop = PropertiesLoaderUtils.loadAllProperties(
				"property/test.properties", ClassUtils.getDefaultClassLoader());
		System.out.println(prop.getProperty("name"));

	}


public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		}
		catch (Throwable ex) {
			// Cannot access thread context ClassLoader - falling back...
		}
		if (cl == null) {
			// No thread context class loader -> use class loader of this class.
			cl = ClassUtils.class.getClassLoader();
			if (cl == null) {
				// getClassLoader() returning null indicates the bootstrap ClassLoader
				try {
					cl = ClassLoader.getSystemClassLoader();
				}
				catch (Throwable ex) {
					// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
				}
			}
		}
		return cl;
	}
第二种方法获取对应的classLoad;


2.property转map:

public static void main(String[] args) throws IOException {
		Properties prop = PropertiesLoaderUtils.loadAllProperties(
				"property/test.properties", ClassUtils.getDefaultClassLoader());
		Map<String, Object> handlerMappings = new ConcurrentHashMap<String, Object>();
		CollectionUtils.mergePropertiesIntoMap(prop, handlerMappings);
		System.out.println(handlerMappings.get("name"));
	}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值