資料的儲存方式除了database之外,還有 properties 可以選擇,優點是使用簡單,但是相對的儲存資料類型較少。
package fothers; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; public class PropertiesExample { public static void main(String[] args){ storeProperties(); loadProperties(); storePropertiesWithXml(); loadPropertiesWithXml(); } public static void storeProperties(){ Properties proper = new Properties(); proper.setProperty("key1", "value1"); try { proper.store(new FileWriter("d:\testproperties\"+"testpro.properties"), "ps"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void loadProperties(){ Properties proper = new Properties(); try { proper.load(new FileReader("d:\testproperties\"+"testpro.properties")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void storePropertiesWithXml(){ Properties proper = new Properties(); proper.setProperty("keyxml", "valuexml"); try { proper.storeToXML(new FileOutputStream("d:\testproperties\"+"testpro.xml"), "ps"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void loadPropertiesWithXml(){ Properties proper = new Properties(); try { proper.loadFromXML(new FileInputStream("d:\testproperties\"+"testpro.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }