ICode9

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

使用Properties集合存储和遍历数据以及Properties集合中的store方法

2022-07-20 10:01:28  阅读:152  来源: 互联网

标签:String properties setProperty 集合 Properties store


使用Properties集合存储和遍历数据

java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>

Properties集合是一个唯一和IO流相结合的集合

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

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

书写列表中每个键及其对应值都是一个字符串。
Properties集合是一个双列集合,key和value默认都是字符串

使用Properties集合存储数据,遍历取出Properties集合中的数据
Properties集合是一个双列集合,key和value默认都是字符串
Properties集合有一些操作字符串特有的方法:
  Object setProperty(String key,String value)调用 Hashtable的方法put。
  String getProperty(String key)通过key找到value值,此方法相当于Map集合中的get(key)方法
  Set<String> stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map集合中的keySet方法

    public static void main(String[] args) {
        //创建Properties结婚
        Properties properties = new Properties();
        //
        properties.setProperty("张三", "150");
        properties.setProperty("李四", "120");
        properties.setProperty("王五", "225");
        properties.setProperty("赵六", "111");
        //使用stringPropertyNames把Properties集合中的键取出,存储到set集合中
        Set<String> set = properties.stringPropertyNames();
        //变量set集合。取出Properties集合的每一个键
        for (String s : set) {
            String property = properties.getProperty(s);
            System.out.println(s+"="+property);
        }
    }

运行结果:

 

 

Properties集合中的store方法

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

  void store(OutputStream out,String comments)

  void store(Writer writer,String comments)

    参数:

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

    Writer writer:字符输出流,可以写中文

    String comments:注释,用来解释说明保存的文件是做什么用的
          不能使用中文,会产生乱码,默认是Unicode编码
          一般使用""空字符串

使用步骤:

  1.创建Properties集合对象,添加数据

  2.创建字节输出流或者字符输出流对象,构造方法中绑定要输出的目的地

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

  4.释放资源

    public static void main(String[] args) throws IOException {
        //创建Properties结婚
        Properties properties = new Properties();
        //使用setProperty往集合中添加数据
        properties.setProperty("张三", "150");
        properties.setProperty("李四", "120");
        properties.setProperty("王五", "225");
        properties.setProperty("赵六", "111");
        //创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
        FileWriter fileWriter = new FileWriter("E:\\a.txt");
        //使用properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
        properties.store(fileWriter, "save data");
        //释放资源
        fileWriter.close();
    }

运行结果:

 

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

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

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

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

ICode9版权所有