ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Properties集合中的store方法,使用Properties集合存储数据 遍历输出Properties中数据

2022-07-19 15:00:35  阅读:136  来源: 互联网

标签:String properties setProperty 集合 Properties store


使用Properties集合存储数据 遍历输出Properties中数据

java.util.Dictionary<K,V> 继承 java.util.Hashtable<Object,Object> 继承 java.util.Properties

Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

 

Properties:集合是一个唯一和iO结合的集合

  可以使用 Properties 集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储

  可以使用 Properties 结合中的方法load,把硬盘中保存的文件(键值对),读取到集合中使用

属性列表中每个键及其对应值都是一个字符串

Properties 集合是一个双列集合,key和Value默认都是字符串

  特有方法:

    setProperty(String key, String value)
          调用 Hashtable 的方法 put

    getProperty(String key)
          用指定的键在此属性列表中搜索属性。

    

案例:

    

public class Properties1 {
public static void main(String[] args) {
// 创建Properties集合对象
Properties properties = new Properties();
properties.setProperty("大傻","1");
properties.setProperty("二傻","2");
properties.setProperty("三傻","3");

// 使用stringPropertyNames把集合中的键取出,存储到set集合当中
Set<String> strings = properties.stringPropertyNames();
for (String ke : strings) {
String va = properties.getProperty(ke);
System.out.println(ke+"="+va);
}
}
}

运行结果:

  

 

 

 

Properties集合中的store方法

    store(OutputStream out ,String comments)

    store(Wreiter wreiter ,String  comments )

  参数:

    OutputStream out:字节输出流,不能写入中文

    Wreiter wreiter :字符输出流,可以写入中文

    String  comments :用来解释保存说明 用来干什么用的(不能使用中文,会产生乱码)

案例:

    

public class Properties1 {
public static void main(String[] args) throws IOException {
// show1();
show2();
}

private static void show2() throws IOException {
// 创建Properties集合对象
Properties properties = new Properties();
properties.setProperty("大傻","1");
properties.setProperty("二傻","2");
properties.setProperty("三傻","3");

FileWriter writer = new FileWriter("aa.txt");
properties.store(writer,"nihao");
writer.close();

}

这样就能写到文本文件中了,我们来看

 

 

 
 

 

标签:String,properties,setProperty,集合,Properties,store
来源: https://www.cnblogs.com/ssr1/p/16494057.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有