properties的store方法
时间: 2023-04-21 17:01:42 浏览: 69
properties的store方法是将属性列表中的键值对存储到输出流中,可以将属性列表保存到文件或其他输出流中。该方法的语法如下:
public void store(OutputStream out, String comments) throws IOException
其中,out表示输出流,comments表示注释信息。如果注释信息为null,则不会添加注释。如果输出流为null,则会抛出NullPointerException异常。如果在写入输出流时发生I/O错误,则会抛出IOException异常。
相关问题
Properties中的store方法
Properties中的store方法是用来将Properties集合中的临时数据持久化写入到硬盘中存储的方法。\[1\]该方法接受一个FileWriter对象和一个字符串作为参数,将Properties集合中的键值对以键=值的形式写入到指定的文件中。\[1\]这样可以将Properties集合中的数据保存到硬盘上,以便下次使用时可以重新加载到集合中。\[2\]在使用store方法时,可以选择追加写入或覆盖写入文件。\[1\]
#### 引用[.reference_title]
- *1* *3* [Properties,Properties的store方法,Properties的load方法](https://blog.csdn.net/qq_40649503/article/details/108696161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [Properties集合中的方法store](https://blog.csdn.net/weixin_44664432/article/details/108545080)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
解析Properties.store
`Properties.store` 方法是用于将属性列表(键和元素都是字符串)写入到输出流中的方法。通常,这个输出流是一个文件输出流或字节输出流。
方法定义如下:
```java
public void store(OutputStream out, String comments) throws IOException
```
其中参数 `out` 是一个字节输出流对象,表示要写入属性列表的输出流,参数 `comments` 是一个字符串,表示注释,可以为null。
`Properties.store` 方法将属性列表以键值对的形式写入到输出流中,格式为:
```
# comments
# 时间戳
key1=value1
key2=value2
...
```
其中,注释部分以`#`开头,时间戳表示写入文件的时间,键和值用`=`分隔。
示例代码如下:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesStoreDemo {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("name", "Tom");
properties.setProperty("age", "18");
try {
FileOutputStream fos = new FileOutputStream("config.properties");
properties.store(fos, "This is a config file");
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们创建了一个 `Properties` 对象,向其中添加了两个属性,然后将其存储到文件 `config.properties` 中,并在文件开头添加了注释 `"This is a config file"`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)