JAVA工具包(二):读取配置文件工具类

  • Post author:
  • Post category:java


1、通过key值调用filePath文件中的值(适用于key、value格式的配置文件):getProp(String key,String filePath);

2、获取配置文件内所有的key及value:getAllProp(String filePath);

3、读取文件内的词语List,适用于单行单词的格式:readFile(String filePath);

4、读取文件内的词语Set,适用于单行单词的格式:readFileSet(String filePath);


import java.io.*;
import java.util.*;

/**
 * @Author Kai
 * @Description 调用自定义配置文件工具类
 * @Date 14:12 2023/3/1
 * @Param
 **/
public class PropertiesUtil {
	private static final String charsetName="UTF-8";
	/**
	 * @Author Kai
	 * @Description 通过key值调用filePath文件中的值(适用于key、value格式的配置文件)
	 * @Date 14:12 2023/3/1
	 * @Param key 文件中的key值
	 * @Param filePath 需调用的文件
	 * @return value内容
	 **/
	public static String getProp(String key,String filePath) {
		InputStreamReader inputStream = null;
		try {
			inputStream = new InputStreamReader(Objects.requireNonNull(PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath)),charsetName);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		Properties prop = new Properties();
		try {
			prop.load(inputStream);
			prop.stringPropertyNames().iterator();
			return prop.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	/**
	 * @Author Kai
	 * @Description 获取配置文件内所有的key及value
	 * @Date 14:20 2023/3/1
	 * @Param filePath 文件名称
	 * @return 返回配置文件集合
	 **/
	public static Map<String, String> getAllProp(String filePath) {
		InputStreamReader inputStream = null;
		try {
			inputStream = new InputStreamReader(Objects.requireNonNull(PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath)),charsetName);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		Properties prop = new Properties();
		try {
			prop.load(inputStream);
			return new HashMap<String, String>((Map) prop);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	/**
	 * @Author Kai
	 * @Description 读取文件内的词语,适用于单行单词的格式
	 * @Date 14:22 2023/3/1
	 * @Param
	 * @return list集合的内容
	 **/
	public static ArrayList<String> readFile(String filePath) {
		String temp;
		File f = new File(filePath);
		// 指定读取编码用于读取中文
		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(f), charsetName);
		
		ArrayList<String> readList = new ArrayList<>();
		BufferedReader reader = new BufferedReader(read);
		while ((temp = reader.readLine()) != null && !"".equals(temp)) {
			readList.add(temp);
		}
		read.close();
		return readList;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (read != null) {
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	/**
	 * @Author Kai
	 * @Description 读取文件内的词语,适用于单行单词的格式
	 * @Date 14:22 2023/3/1
	 * @Param
	 * @return set集合的内容
	 **/
	public static Set<String> readFileSet(String filePath) {
		String temp;
		File f = new File(filePath);
		// 指定读取编码用于读取中文
		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(f),charsetName);
			Set<String> readList = new HashSet<>();
			BufferedReader reader = new BufferedReader(read);
			while ((temp = reader.readLine()) != null && !"".equals(temp)) {
				readList.add(temp);
			}
			read.close();
			return readList;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (read != null) {
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
}



版权声明:本文为weixin_38763887原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。