基于IO实现的properties文件读取工具类

该博客介绍了一个方便对properties文件进行读取的工具类,能有效提升对properties文件的操作效率,属于信息技术领域中文件处理相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方便对properties文件读取的工具类

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * <p>
 *	PropertiesFileReadUtil.java
 *	properties文件读取工具
 * </p>
 * @since 2019年2月28日 上午8:56:09
 * @author XinLau
 * @version 1.0
 */
public class PropertiesFileReadUtil {
	
	/**
	 * <p>
	 *	getPropertiesMap
	 *	获取properties文件
	 * </p>
	 * @param filePath
	 * @return
	 * @return Map<String,String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:32:31
	 */
	public static Map<String,String> getPropertiesMap(String filePath){
		Properties prop = new Properties();
		try {
			//	通过输入缓冲流读取properties文件
			InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
			//	加载输入流
			prop.load(inputStream);
			return PropertiesMap(prop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * <p>
	 *	getPropertiesKeyList
	 *	获取properties文件的Key
	 * </p>
	 * @param filePath
	 * @return List<String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:35:33
	 */
	public static List<String> getPropertiesKeyList(String filePath){
		Properties prop = new Properties();
		try {
			//	通过输入缓冲流读取properties文件
			InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
			//	加载输入流
			prop.load(inputStream);
			return KeyList(prop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * <p>
	 *	getPropertiesValueList
	 *	获取properties文件的Value
	 * </p>
	 * @param filePath
	 * @return List<String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:36:05
	 */
	public static List<String> getPropertiesValueList(String filePath){
		Properties prop = new Properties();
		try {
			//	通过输入缓冲流读取properties文件
			InputStream inputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
			//	加载输入流
			prop.load(inputStream);
			return ValueList(prop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * <p>
	 *	PropertiesFileRead
	 *	返回properties文件的MAP集合
	 * </p>
	 * @param props	properties文件
	 * @return Map<String,String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:12:55
	 */
	private static Map <String,String> PropertiesMap(Properties props){
		Map <String,String> map = new HashMap<>();
		Enumeration<?> en = props.propertyNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			String value = props.getProperty(key);
			map.put(key, value);
		}
		return map;
	}
	
	/**
	 * <p>
	 *	KeyList
	 *	返回properties文件的Key数组
	 * </p>
	 * @param props	properties文件
	 * @return List<String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:18:19
	 */
	private static List<String> KeyList(Properties props){
		List <String> keyList = new ArrayList<String>();
		Enumeration<?> en = props.propertyNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			keyList.add(key);
		}
		return keyList;
	}
	
	/**
	 * <p>
	 *	KeyList
	 *	返回properties文件的Value数组
	 * </p>
	 * @param props	properties文件
	 * @return List<String>
	 * @author XinLau
	 * @since 2019年2月28日上午9:18:19
	 */
	private static List<String> ValueList(Properties props){
		List <String> valueList = new ArrayList<String>();
		Enumeration<?> en = props.propertyNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			String value = props.getProperty(key);
			valueList.add(value);
		}
		return valueList;
	}

	public static void main(String[] args) {
		String PROPERTIESFILEPATH = "/imis-config/src/main/resources/jdbc.properties";
		Map<String,String> A = PropertiesFileReadUtil.getPropertiesMap(PROPERTIESFILEPATH);
		List <String> keyList = PropertiesFileReadUtil.getPropertiesKeyList(PROPERTIESFILEPATH);
		List <String> valueList = PropertiesFileReadUtil.getPropertiesValueList(PROPERTIESFILEPATH);
		
		for (String key : keyList) {
			String value = A.get(key);
			System.out.println(key + ":" + value);
		}
		
		for (String value : valueList) {
			System.out.println(value);
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叁金Coder

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

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

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

打赏作者

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

抵扣说明:

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

余额充值