properties可以load store
注释可以采用 "#" 或者"!"
分隔采用"="或者":"
分行采用" "
由于property类继承的是hashtable,可以采用put putall 但是不推荐
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Properties props = new Properties(); InputStream input = new FileInputStream(new File("D:/javaDevelop/oddjob-1.4.0-SNAPSHOT-src/oddjob-src/oddjob/src/target/classes/my.properties")); props.load(input); input.close(); System.out.println(props.getProperty("aa")); System.out.println(props.getProperty("bb")); System.out.println(props.getProperty("cc")); OutputStream os =System.out; props.storeToXML(os, "fdasfsa"); props.store(os, "putong"); props.list(System.out); }
1 package com.zuidaima; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.util.Properties; 8 9 public class Main { 10 11 public static void main(String[] args) { 12 Properties property = new Properties(); 13 try { 14 File file = new File("c:/db.properties"); 15 if (!file.exists()) { 16 file.createNewFile(); 17 } 18 // 写入 19 property.setProperty("database", "localhost"); 20 property.setProperty("user", "zuidaima"); 21 property.setProperty("password", "password"); 22 property.store(new FileOutputStream(file), null); 23 24 property.load(new FileInputStream(file)); 25 26 // 读取 27 System.out.println(property.getProperty("database")); 28 System.out.println(property.getProperty("user")); 29 System.out.println(property.getProperty("password")); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 } 35 36