java操作properties(关注properties的路径)

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");


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值