ahomeeye 发表于 2013-1-28 18:22:03

properties配置文件的读写类

先贴出代码,有空再来写说明。
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Set;public class SingleProperty {    private Properties prop = null;    private InputStream in = null;    private OutputStream os;    private String path;    public SingleProperty(String path) {      this.path = path;      initProperty();    }    public SingleProperty() {      prop = new Properties();    }    public void setPath(String path) {      try {            in = new FileInputStream(this.path);      } catch (FileNotFoundException ex) {      }    }    private void initProperty() {      try {            prop = new Properties();            //System.getProperty("user.dir") 获得项目所在位置路径,如 F:\zgh\netbeanspace\ClientTest            //path=conf/config.properties 此位置为项目所在路径下的conf/config.properties文件路径,conf与lib并列            in = new FileInputStream(this.path);      } catch (FileNotFoundException ex) {      }    }    private void setIs() {      try {            if (in == null) {                in = new FileInputStream(this.path);            }      } catch (IOException ex) {      }    }    private void setOs() {      try {            os = new FileOutputStream(this.path); //"src/org/lt/cj/config/config.properties"      } catch (FileNotFoundException ex) {      }    }    //获取所有属性    public Properties getProperty() {      try {            setIs();            prop.load(in);      } catch (IOException ex) {      }      return prop;    }    //获取属性值    public String getValue(String key) {      String value = "";      try {            setIs();            prop.load(in);            value = prop.getProperty(key);      } catch (IOException ex) {      }      return value;    }    //将Map的键值对添加到配置文件,值允许用空字符    public void addProperty(Map<String, String> map, String comments) {      try {            setIs();            prop.load(in);            setOs();            Set keys = map.keySet();            for (Iterator it = keys.iterator(); it.hasNext();) {                String key = (String) it.next();                String value = map.get(key);                prop.setProperty(key, value);            }            prop.store(os, comments);      } catch (IOException ex) {      }    }    //修改属性值    public void setProperty(String key, String value) {      prop.setProperty(key, value);    }    //保存配置文件    public void store(String comments) {      try {            setOs();            prop.store(os, comments);      } catch (IOException ex) {      }    }    //删除key所指向的属性值    public void deleteProperty(String key, String comments) {      try {            setIs();            prop.load(in);            prop.remove(key);            setOs();            prop.store(os, comments);      } catch (IOException ex) {      }    }    //关闭读配置文件的IO    public void endAndcloseIO() {      try {            if (os != null) {                os.flush();                os.close();            }            if (in != null) {                in.close();            }      } catch (IOException ex) {      }    }    //打印配置文件中的所有属性    public void printMap(Map map) {      Set keys = map.keySet();      for (Iterator it = keys.iterator(); it.hasNext();) {            Object key = it.next();            Object value = map.get(key);            System.out.println(key + "==" + value);      }    }    public static void main(String[] args) throws Exception {      SingleProperty p = new SingleProperty("src/com/test/config.properties");      Map<String, String> map = new HashMap<String, String>();      map.put("myParam", "hello");      map.put("parameter2", "Hi,nice to meet you.");      p.addProperty(map, "修改配置参数");    }}
页: [1]
查看完整版本: properties配置文件的读写类