当前位置: 代码迷 >> 综合 >> java封装一个操作properties配置文件的类
  详细解决方案

java封装一个操作properties配置文件的类

热度:15   发布时间:2024-02-07 06:30:11.0
package cn.tools;import java.io.*;
import java.util.Properties;public class ProUtil {private final Properties prop;private final String filePath;//构造方法public ProUtil(String string) {this.filePath = string;this.prop = readProperties();}//读取key值private Properties readProperties() {Properties properties = new Properties();try {InputStream inputStream = new FileInputStream(filePath);BufferedInputStream in = new BufferedInputStream(inputStream);properties.load(in);} catch (IOException e) {e.printStackTrace();}return properties;}//获取配置文件public String getPro(String key) {if (prop.containsKey(key)) {return prop.getProperty(key);} else {System.out.println("没有获取到" + key + "值");return "";}}//简单读取配置文件public static String get(String path, String key) throws IOException {Properties properties = new Properties();// 使用InPutStream流读取properties文件BufferedReader in = new BufferedReader(new FileReader(path));properties.load(in);//获取key对应的value值return properties.getProperty(key);}
}