定义:Properties 继承于 Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。
package com.fs.myProperties;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
BufferedInputStream in = new BufferedInputStream(new FileInputStream("test.properties"));
properties.load(in);
Enumeration<?> keys = properties.propertyNames();
while (keys.hasMoreElements()) {
Object key = (Object) keys.nextElement();
System.out.println(key + ":" + properties.get(key));
}
in.close();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.properties"));
properties.setProperty("sex", "man");
properties.store(out, "sex");
out.close();
}
}
#sex
#Tue Oct 16 17:19:39 CST 2018
age=18
name=lzl
sex=man