------------------------------------------------------
Properties类的重要方法
Properties 类存在于胞 Java.util 中,该类继承自 Hashtable
1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文
件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素
对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。
-------------------------------
public class PropertiesTest {
public static void main(String[] args) {
Properties p=new Properties();
try {
InputStream is=new PropertiesTest().getClass().getResourceAsStream("/config/db.properties");
p.load(is);
String s=(String) p.get("username");
System.out.println(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
属性文件放在classpath下面:即项目的src目录下面!