1、获取指定键的值:
/**
*
* 一句话简单描述:获取指定的key的value 详细说明:方法执行需要注意properties的路径
* 1、工程中测试获取可使用:System.getProperty("user.dir") + "...." 2、tomcat获取路径:...
*
* @param filePath
* properties文件的路径。
* @param paramKey
* 键
* @return 返回指定键的值
* @throws IOException
* 文件流操作的IO异常
*/
public String readParamValue(String filePath, String paramKey)
throws IOException {
// 定义一个属性类
Properties properties = new Properties();
// 采用BufferedInputStream读取文件
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
// 将文件加载到到Properties中
properties.load(in);
// 返回获取键的值
return properties.getProperty(paramKey);
}
2、添加、修改指定的键值队:
/**
*
* @一句话简单描述:添加、修改键值对属性
* @param filePath
* properties文件的路径。
* @param paramKey
* 键
* @param paramValue
* 值
* @return void 无返回
* @throws 文件流操作的IO异常
*/
public void addPropertie(String filePath, String paramKey, String paramValue)
throws IOException {
// 定义一个属性类
Properties properties = new Properties();
// 创建一个文件输入流
InputStream fis = new FileInputStream(filePath);
// 将文件加载到到Properties中
properties.load(fis);
// 创建一个文件输出流
OutputStream fos = new FileOutputStream(filePath);
// 设置属性
properties.setProperty(paramKey, paramValue);
// 调用store方法保存属性
properties.store(fos, "ADD" + paramKey + "VALUE");
}