java删除配置文件键值_Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作...

packagecn.xm.exam.utils;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Properties;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/***

*@authorQiaoLiQiang

* @time 2018年11月3日下午12:05:32*/

public classPropertiesFileUtils {private static final Logger log = LoggerFactory.getLogger(PropertiesFileUtils.class);/*** 构造函数私有化*/

privatePropertiesFileUtils() {

}/*** 保存或更新properties文件中的key

*

*@paramfileName

*@paramkey

*@paramvalue*/

public static voidsaveOrUpdateProperty(String fileName, String key, String value) {

Properties properties= newProperties();

InputStream inputStream;

OutputStream outputStream;try{

String path= ResourcesUtil.class.getClassLoader().getResource(fileName).getPath();

log.debug("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

properties.setProperty(key, value);//保存到文件中(如果有的话会自动更新,没有会创建)

outputStream = new FileOutputStream(newFile(path));

properties.store(outputStream,"");

outputStream.close();

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}

}/*** 获取Properties

*

*@paramfileName

*@paramkey

*@return

*/

public staticString getPropertyValue(String fileName, String key) {

Properties properties= newProperties();

InputStream inputStream;

String value= "";try{

String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

value=properties.getProperty(key);//保存到文件中(如果有的话会自动更新,没有会创建)

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnvalue;

}/*** 获取Properties

*

*@paramfileName

*@return

*/

public staticProperties getProperties(String fileName) {

Properties properties= newProperties();

InputStream inputStream;try{

String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnproperties;

}/*** 获取Properties

*

*@paramfileName

*@return

*/

public staticProperties removeProperty(String fileName, String key) {

Properties properties= newProperties();

InputStream inputStream;

OutputStream outputStream;try{

String path= PropertiesFileUtils.class.getClassLoader().getResource(fileName).getPath();

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

log.info("properties -> {}", properties);if (properties != null &&properties.containsKey(key)) {

log.info("remove key:{}", key);

properties.remove(key);

}//保存到文件中(将properties保存到文件)

outputStream = new FileOutputStream(newFile(path));

properties.store(outputStream,"");

outputStream.close();

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnproperties;

}/*** 保存或更新properties文件中的key

*

*@parampath

* 文件全路径

*@paramkey

*@paramvalue*/

public static voidsaveOrUpdatePropertyByFilePath(String path, String key, String value) {

Properties properties= newProperties();

InputStream inputStream;

OutputStream outputStream;try{

log.debug("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

properties.setProperty(key, value);//保存到文件中(如果有的话会自动更新,没有会创建)

outputStream = new FileOutputStream(newFile(path));

properties.store(outputStream,"");

outputStream.close();

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}

}/*** 获取Properties

*

*@parampath

* 文件全路径

*@paramkey

*@return

*/

public staticString getPropertyValueByFilePath(String path, String key) {

Properties properties= newProperties();

InputStream inputStream;

String value= "";try{

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

value=properties.getProperty(key);//保存到文件中(如果有的话会自动更新,没有会创建)

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnvalue;

}/*** 获取Properties

*

*@parampath

* 文件全路径

*@return

*/

public staticProperties getPropertiesByFilePath(String path) {

Properties properties= newProperties();

InputStream inputStream;try{

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnproperties;

}/*** 获取Properties

*

*@parampath

* 文件全路径

*@paramkey

* key值

*@return

*/

public staticProperties removePropertyByFilePath(String path, String key) {

Properties properties= newProperties();

InputStream inputStream;

OutputStream outputStream;try{

log.info("path -> {}", path);

inputStream= new FileInputStream(newFile(path));

properties.load(inputStream);

log.info("properties -> {}", properties);if (properties != null &&properties.containsKey(key)) {

log.info("remove key:{}", key);

properties.remove(key);

}//保存到文件中(将properties保存到文件)

outputStream = new FileOutputStream(newFile(path));

properties.store(outputStream,"");

outputStream.close();

inputStream.close();

}catch(FileNotFoundException e) {

log.error("saveOrUpdateProperty error", e);

}catch(IOException e) {

log.error("saveOrUpdateProperty error", e);

}returnproperties;

}}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值