对properties文件的读写操作

1.对properties文件的读写操作


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class ConfigProperty {
private Properties properties;
private String filePath;
/**
* 构造方法加载配制文件
*
* @param path
*/
public ConfigProperty(String path) {
properties = new Properties();
filePath = path;
try {
properties.load(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 读取属性文件中相应键的值
*
* @param key
* 主键
* @return String
*/
public String getpropertiesValue(String key) {
return properties.getProperty(key);
}

/**
* 更新(或插入)一对properties信息(主键及其键值) 如果该主键已经存在,更新该主键的值; 如果该主键不存在,则插件一对键值。
*
* @param key
* 键名
* @param value
* 键值
*/
public void setProperties(String key, String value) {
try {
OutputStream fos = new FileOutputStream(filePath);
properties.setProperty(key, value);
properties.store(fos,null);
} catch (IOException e) {
System.out.println(e.getMessage());

}

}

public void deleteProperties(String path) throws IOException{
File file=new File(path);
if(file.exists()){
file.delete();
}else {
throw new IOException("没有找到文件");
}
}


public static void main(String[] args) {
ConfigProperty config = new ConfigProperty("E:\\workspace5.5\\oa\\src\\jdbc.properties");
String s = config.getpropertiesValue("jdbc.url");
System.out.println(s);
config.setProperties("MAIL_SERVER_INCOMING", "327@qq.com");

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值