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保留下来。
本文深入解析Properties文件的使用方法,包括如何读取和写入文件中的键值对,提供了完整的示例代码,适用于Java编程场景。
336

被折叠的 条评论
为什么被折叠?



