如何通过java代码在属性文件中写入值 [英] How to write values in a properties file through java code

查看:99
本文介绍了如何通过java代码在属性文件中写入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。

我有一个属性文件。我想在该文件中存储一些值,并在需要时在代码中实现。有没有办法做到这一点?

I have a properties file. I want to store some values in that file and will implement in the code whenever it is required. Is there any way to do that?

我正在使用属性类来做这件事..

I am using Properties class to do that..

推荐答案

使用 java.util.Properties

代码段 -

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("xyz.properties");
prop.load(in);

它提供 Properties#setProperty(java.lang。 String,java.lang.String) 有助于添加新属性。

It provides Properties#setProperty(java.lang.String, java.lang.String) which helps to add new property.

代码段 -

prop.setProperty("newkey", "newvalue");

您可以使用 属性#store (java.io.OutputStream,java.lang.String)

This new set you can save using Properties#store(java.io.OutputStream, java.lang.String)

Code Snippet -

Code Snippet -

prop.store(new FileOutputStream("xyz.properties"), null);

这篇关于如何通过java代码在属性文件中写入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆