Properties 继承于Hashtable
他可用流做参数 FileInputStream 和 FileOutputStream
常用方法:load、store、getProperty、setProperty
以下为实例代码:
package dirk.property; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; import java.util.Map.Entry; /** * @author logichina * */ /** * @author logichina * */ public class ReadProperty { /*public static void main(String[] args) { Properties pps = System.getProperties(); pps.list(System.out); }*/ public static void main(String[] args) { Properties pps = new Properties(); try { pps.load(new FileInputStream(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties")); //Enumeration<Object> enum1 = pps.propertyNames(); Iterator<Entry<Object, Object>> ppsi = pps.entrySet().iterator(); while(ppsi.hasNext()) { Entry<Object,Object> e = ppsi.next(); String strKey = (String)e.getKey(); String strValue = (String)e.getValue(); System.out.println(strKey+":"+strValue+"--"); } pps.setProperty("new info","chenchen"); pps.setProperty("dirk.team", "dallasForever"); //writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.information","context","dirk.zhang modify"); //writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.boss","still ku.ban","dirk.zhang modify"); pps.store(new FileOutputStream(System.getProperty("user.dir")+"/src/test.out"), "new dirk information"); }catch(Exception e) { e.printStackTrace(); } } }
以下是封装后的实例代码
package dirk.property; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; import java.util.Map.Entry; /** * @author logichina * */ /** * @author logichina * */ public class ReadProperty { /*public static void main(String[] args) { Properties pps = System.getProperties(); pps.list(System.out); }*/ public static void main(String[] args) { Properties pps = new Properties(); try { pps.load(new FileInputStream(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties")); //Enumeration<Object> enum1 = pps.propertyNames(); Iterator<Entry<Object, Object>> ppsi = pps.entrySet().iterator(); while(ppsi.hasNext()) { Entry<Object,Object> e = ppsi.next(); String strKey = (String)e.getKey(); String strValue = (String)e.getValue(); System.out.println(strKey+":"+strValue+"--"); } pps.setProperty("new info","chenchen"); pps.setProperty("dirk.team", "dallasForever"); writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.information","context","dirk.zhang modify"); writeProperty(System.getProperty("user.dir")+"/src/dirk.zhang.jsfdemo.properties","dirk.boss","still ku.ban","dirk.zhang modify"); //pps.store(new FileOutputStream(System.getProperty("user.dir")+"/src/test.out"), "new dirk information"); }catch(Exception e) { e.printStackTrace(); } } public static void readPropertyToObj(String path,PropertyInfo infoObj) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream(path)); /*封装对象*/ infoObj.setInfo(pps); } public static Properties readPropertyToPps(String path) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream(path)); return pps; } public static void writeProperty(String path,String name,String value,String note) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream(path)); pps.setProperty(name, value); pps.store(new FileOutputStream(path), note); } /** * property obj for dirk * @author dirk.zhang */ class DirkObj implements PropertyInfo { public final String NAME="name"; public final String NUM="num"; public final String BOSS="boss"; private String name=""; private String num=""; private String boss=""; public String setInfo(Properties pps) { // TODO Auto-generated method stub if(pps==null) return "error no such property obj"; name = pps.getProperty(NAME); num = pps.getProperty(NUM); boss = pps.getProperty(BOSS); boss = pps.getProperty("没有"); return ""; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getBoss() { return boss; } public void setBoss(String boss) { this.boss = boss; } @Override public String toString() { //System.out.println(this.name+":"+this.num+" "+this.boss); return this.name+":"+this.num+" "+this.boss; } } }
还定义一个文件和bean交互的接口
package dirk.property; import java.util.Properties; /** * property obj interface * @author dirk.zhang */ public interface PropertyInfo { String setInfo(Properties pps); }