zoukankan      html  css  js  c++  java
  • 属性集[Properies] 特殊的集合 能够配合IO流来使用的特殊属性集合类

    属性集[Properies]

    java.util.properties 类继承与Hashtable,用来表示一个持久的属性集.它使用的键值结构存储数据,每个键及其对应的值都是一个字符串.

    构造方法

    • public Properties():创建一个空的属性集列表

    共性的api方法

    • public Object setProperty(String key,String value):保存一对属性.

    • public String getProperty(String key):使用此属性列表中的指定的键来搜索对应的值.

    • public Set<String> stringPropertyNames():获取所有键的名称并封装Set集合中.

      /创建属性集对象
              Properties pro = new Properties();
              //添加键值对元素
             pro.setProperty("name", "abc.txt");
             pro.setProperty("size", "12000");
             pro.setProperty("destination", "D\abc.txt");
             pro.put("date","小孙");
             System.out.println(pro);
             //通过键找值来获取值
              Object date = pro.get("date");
             System.out.println(date);
              String size = pro.getProperty("size");
             System.out.println(size);
             //遍历该属性集
             //public Set<String> stringPropertyNames():获取所有键的名称并封装Set集合中.
             for (String st : pro.stringPropertyNames()) {
                 System.out.println(pro.getProperty(st));
            }

       

    与流相关的方法

    • public void load(inputSream input):从字节输入流中读取

    参数中使用了字节输入流,通过流对象,可以关联到某个文件上,这样就可以加载文件中的数据的格式:

    "key=value"

    例如:
    date=小孙
    size=1200
    name=abc.txt

    可以使用Properties集合中的load方法对输入流进行操作,把硬盘文件中的数据读取出来,保存到集合 properties当中使用 void load(InputStream input):从字节输入流中读取文件中的键值对 void load(Writer read):从字符输入流中读取文件中的键值对 参数: inputStream input:字节输入流,不能读取含有中文的键值对 Reader reader:字符输入流,可以读取含有中文的键值对 使用步骤: 1.创建Properties集合 2.使用Properties集合中的方法load读取保存在输入流中的数据 3.遍历properties集合

    注意:

    1.在存储键值对的文件中,键与值默认的连接符号是"=",可以使用空格(其他符号)

    2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会被读取到.

     FileInputStream f = new FileInputStream("day29_IO\abc.txt");
           //1.创建Properties集合
           Properties pro = new Properties();
           //2.使用Properties集合中的方法load读取保存在输入流中的数据
           pro.load(f);
           //3.遍历Properties集合
           for (String st : pro.stringPropertyNames()) {
               System.out.println(pro.getProperty(st));
          }
           //12000
    • public void store(OutputStream out,String comments):把集合当中数据写入字节输出流中

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

           //创建Properties集合对象.添加数据
           Properties pro = new Properties();
           pro.setProperty("四大名著1", "红楼梦");
           pro.setProperty("四大名著2", "西游记");
           pro.setProperty("四大名著3", "水浒传");
           pro.setProperty("四大名著4", "三国演义");
        //   2.创建字节输出流/字节输出流对象,构造方法中绑定需要写入数据的目的地
          FileWriter fw = new FileWriter("day29_IO\avcd.txt");
          pro.store(fw,"si da ming zhu");//注释不能用中文会乱码
          //释放资源
          fw.close();
  • 相关阅读:
    高阶篇:1.2)材料和工艺的选择
    高阶篇:4.1.1)QFDI(客户需求转换为设计要求)
    高阶篇:4)可靠性设计-总章
    高阶篇:1.5)如何选择更好的概念-Pugh矩阵法
    知识点篇:2)产品结构设计目标的分类
    高阶篇:1.1)竞品(标杆产品)的拆解和分析benchmarking
    支持向量机
    机器学习概述
    HDU_oj_2055 An easy problem
    HDU_oj_2054 A==B ?
  • 原文地址:https://www.cnblogs.com/rosiness/p/14175921.html
Copyright © 2011-2022 走看看