读书人

对properties资料的读写操作

发布时间: 2012-08-27 21:21:57 作者: rapoo

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

读书人网 >编程

热点推荐