`
rocketa
  • 浏览: 23965 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java的properties读取和写入

    博客分类:
  • Java
 
阅读更多

properties应该算是比较常用的东西了,它能自动把文件中的行转换成键值对的关系,提供给用户使用。

读取:

Properties p=new Properties(); 
p.load(new FileInputStream(file));
String userName = p.getProperty("userName");

 当然,其中的new FileInputStream(file)可以替换成你需要的形式。

 

写入:

Properties p=new Properties(); 
p.load(new FileInputStream(file));
p.put("userName", "rocketa");
p.store(new FileOutputStream(file), "comment");

 其中第2行先load一次,为的是在写文件的时候把原来的properties保留下来。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics