读
//实例化Properties对象
Properties p =new Properties();
try {
;
//加载一个Properties文件(通过当前类名点Class里面的getgetResourceAsStream方法来读取文件路径)
p.load(test.class.getResourceAsStream("/UserName.properties"));
//通过键获取Properties值然后输出
System.out.println(p.getProperty("userName"));
System.out.println(p.getProperty("passWord"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
写
//实例化Properties对象
Properties p=new Properties();
try {
//给Property对象添加属性集
p.setProperty("name", "梁文");
p.setProperty("sex", "男");
p.setProperty("age", String.valueOf(20));
//把属性集写入到Properties 文件里面
p.store(new FileWriter("D:\\temp\\wzy.properties"), "This is comment");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}