一、向Properties中添加属性
public Object setProperty(String key,
String value)
put
。使用getProperty 方法提供并行性。强制要求为属性的键和值使用字符串。返回值是Hashtable 调用put
的结果。
key
- 要置于属性列表中的键。
value
- 对应于 key 的值。
null
。
public static void generateProperties(Properties properties){ properties.setProperty("company", "companyName"); properties.setProperty("department", "departmentName"); properties.setProperty("position", "positonName"); properties.setProperty("name", "Name"); }
public String getProperty(String key)
null
。
key
- 属性键。
public String getProperty(String key,
String defaultValue)
key
- 哈希表键。
defaultValue
- 默认值。
三、从文件中载入Properties
public void load(Reader reader)
throws IOException
此方法返回后,指定的流仍保持打开状态。
reader
- 输入字符流。
public void load(InputStream inStream)
throws IOException
load(Reader)
中所指定的、简单的面向行的格式。
此方法返回后,指定的流仍保持打开状态。
inStream
- 输入流。
public static void loadPropertiesFromFile(File propertiesFile, Properties properties){ InputStream in = null; try{ in = new FileInputStream(propertiesFile); properties.load(in); //第一种方法遍历Properties: //use Enumeration to visit the properties System.out.println("propertyNames()"); Enumeration<?> enumeration = properties.propertyNames(); while(enumeration.hasMoreElements()){ String value = (String) enumeration.nextElement(); System.out.println(value + "=" + properties.getProperty(value)); } System.out.println(); //第二种方法遍历Properties: //use KeySet to visit the properties System.out.println("KeySet()"); Set<Object> keyset = properties.keySet(); Iterator<Object> itr = keyset.iterator(); while(itr.hasNext()){ String key = (String) itr.next(); System.out.println(key + "=" + properties.getProperty(key)); } System.out.println(); //第三种方法遍历Properties: //use stringPropertyNames() to visit the properties System.out.println("stringPropertyNames() "); Set<String> keysetNew = properties.stringPropertyNames(); Iterator<String> itrNew = keysetNew.iterator(); while(itrNew.hasNext()){ String key = itrNew.next(); System.out.println(key + "=" + properties.getProperty(key)); } }catch(Exception e){ e.printStackTrace(); } }四、存储Properties到文件
public void store(Writer writer,
String comments)
throws IOException
Properties
表中的属性列表(键和元素对)写入输出字符。
comments 为自定义的properties文件注释;接下来再默认写入一个注释行,该行包括一个 ASCII#
字符、当前的日期和时间。然后将此Properties
表中的所有项写入 out,一次一行。写入各个项后,刷新输出流。此方法返回后,输出流仍保持打开状态。
writer
- 输出字符流 writer。
comments
- 属性列表的描述。
public void store(OutputStream out,
String comments)
throws IOException
Properties
表中的属性列表(键和元素对)写入输出流。
此方法以 store(Writer)
中指定的相同格式输出注释、属性键和值。
out
- 输出流。
comments
- 属性列表的描述。
示例:
public static void saveProperties2file(File propertiesFile, Properties properties){ OutputStream os = null; try { os = new FileOutputStream(propertiesFile); properties.store(os, "properties write test"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
补充:
这里properties保存为properties.property键值对文件格式,当然properties也可以保存为xml文件格式(API为storeToXML,对应的读取API为
loadFromXML详见jdk文档
)。
五、完整测试程序
public class PropertiesTest { public static void main(String[] args) { //generate a new properties content Properties properties = new Properties(); generateProperties(properties); //create properties file File propertiesFile = createPropertiesFile(); //save properties to file saveProperties2file(propertiesFile, properties); //load/read properties from file loadPropertiesFromFile(propertiesFile, properties); } //save properties to file public static void saveProperties2file(File propertiesFile, Properties properties){ OutputStream os = null; try { os = new FileOutputStream(propertiesFile); properties.store(os, "properties write test"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //load/read properties from file public static void loadPropertiesFromFile(File propertiesFile, Properties properties){ InputStream in = null; try{ in = new FileInputStream(propertiesFile); properties.load(in); //第一种方法遍历Properties: //use Enumeration to visit the properties System.out.println("propertyNames()"); Enumeration<?> enumeration = properties.propertyNames(); while(enumeration.hasMoreElements()){ String value = (String) enumeration.nextElement(); System.out.println(value + "=" + properties.getProperty(value)); } System.out.println(); //第二种方法遍历Properties: //use KeySet to visit the properties System.out.println("KeySet()"); Set<Object> keyset = properties.keySet(); Iterator<Object> itr = keyset.iterator(); while(itr.hasNext()){ String key = (String) itr.next(); System.out.println(key + "=" + properties.getProperty(key)); } System.out.println(); //第三种方法遍历Properties: //use stringPropertyNames() to visit the properties System.out.println("stringPropertyNames() "); Set<String> keysetNew = properties.stringPropertyNames(); Iterator<String> itrNew = keysetNew.iterator(); while(itrNew.hasNext()){ String key = itrNew.next(); System.out.println(key + "=" + properties.getProperty(key)); } }catch(Exception e){ e.printStackTrace(); } } //create properties file public static File createPropertiesFile(){ File propertiesDir = null; File propertiesFile = null; try { String userDir = System.getProperty("user.dir"); propertiesDir = new File(userDir + File.separator + "config" ); System.out.println(propertiesDir); if(!propertiesDir.exists()){ propertiesDir.mkdirs(); } propertiesFile = new File(propertiesDir + File.separator + "properties.property"); System.out.println(propertiesFile); if(!propertiesFile.exists()){ propertiesFile.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } return propertiesFile; } public static void generateProperties(Properties properties){ properties.setProperty("company", "companyName"); properties.setProperty("department", "departmentName"); properties.setProperty("position", "positonName"); properties.setProperty("name", "Name"); } }
#properties write test #Wed Mar 02 16:01:14 CST 2016 position=positonName company=companyName name=Name department=departmentName
补充:
当保存为xml文件格式时,其内容为
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>properties write test</comment> <entry key="position">positonName</entry> <entry key="company">companyName</entry> <entry key="department">departmentName</entry> <entry key="name">Name</entry> </properties>