Java程序设计 笔记(3)
Properties
#add value,mp5 #Tue Mar 26 21:18:42 CST 2013 #3、properties文件的一个属性配置信息值可以换行,但键不可以换行。值换行用“\”表示。 #4、properties的属性配置键值前后的空格在解析时候会被忽略。 #5、properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配#置不能没有键。 MP4=MP4 MP3=MP3 mp5=mp5
// 读一个value public String readPropertiesValue(String filePath, String key) { Properties p = new Properties(); try { p.load(new FileInputStream(filePath)); } catch(IOException e) {} return p.getProperty(key); } //写一对key和value public void writePropertiesKeyAndValue(String filePath, String key, String value) { Properties p = new Properties(); try { p.load(new FileInputStream(filePath)); p.setProperty(key, value); p.store(new FileOutputStream(filePath), "add value," + value); } catch(IOException e) {} } //读全部的value public void readProperties(String filePath) { Properties p = new Properties(); try { p.load(new FileInputStream(filePath)); } catch(IOException e) {} Set keys = p.keySet(); Iterator it = keys.iterator(); while(it.hasNext()) { String keytemp = it.next().toString(); System.out.println("key=" + keytemp + ",value=" + p.getProperty(keytemp)); } }
public static void main(String [] args) { Second s = new Second(); String value = s.readPropertiesValue("D:\\p.properties", "MP3"); System.out.println(value); s.writePropertiesKeyAndValue("D:\\p.properties", "mp5", "mp5"); s.readProperties("D:\\p.properties"); }
工厂模式
仰望天空,脚踏实地
浙公网安备 33010602011771号