java properties类怎么使用

31次阅读
没有评论

共计 699 个字符,预计需要花费 2 分钟才能阅读完成。

在 Java 中,可以通过 Properties 类来读取和写入属性文件。下面是 Properties 类的使用示例:

  1. 创建 Properties 对象并加载属性文件:
Properties properties = new Properties();
try {FileInputStream fileInputStream = new FileInputStream("config.properties");
properties.load(fileInputStream);
fileInputStream.close();} catch (IOException e) {e.printStackTrace();
}
  1. 读取属性值:
String value = properties.getProperty("key");
System.out.println(value);
  1. 设置属性值:
properties.setProperty("key", "value");
  1. 保存属性文件:
try {FileOutputStream fileOutputStream = new FileOutputStream("config.properties");
properties.store(fileOutputStream, "Config Properties");
fileOutputStream.close();} catch (IOException e) {e.printStackTrace();
}

以上就是使用 Properties 类读取和写入属性文件的基本步骤。需要注意的是,属性文件的格式为键值对,如 ”key=value”,每个属性占一行。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-20发表,共计699字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)