• Properties类随笔


    1. 体系介绍

      Properties类继承自HashTable,勉强属于集合框架体系一员,键值对形式存储数据,当然键肯定是唯一的,底层哈希表保证键的唯一,此类一般用于表示配置文件.

    2. 基本用法

      由于此类继承HashTable,所以基本的集合操作还是相同的.比如put(),get()等,当然这个肯定不是常用的,在此只介绍Properties特殊的功能.

    •  Object setProperty(String key, String value)  : 存入数据.严格要求键值对类型都是字符串
    •  String getProperty(String key)  :按照键取数据,.严格要求键值对类型都是字符串
    •  Enumeration<?> propertyNames()   :获取所有键的列表
    •  Set<String> stringPropertyNames() : 获取所有键的列表
    •  void store(OutputStream out, String comments)  : 与IO结合,使用字节输出流以键值对形式持久到磁盘,第二个参数是描述..可null
    •  void store(Writer writer, String comments)   : 与IO结合,使用字符输出流以键值对形式持久到磁盘,第二个参数是描述..可null
    •  void load(InputStream inStream)   : 与IO结合,使用字节输入流从磁盘加载到Properties对象中
    •  void load(Reader reader)    : 与IO结合,,使用字符输入流从磁盘加载到Properties对象中

      

    package com.io;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.Writer;
    import java.util.Enumeration;
    import java.util.Properties;
    import java.util.Set;
    
    /**
     * 演示Properties类的使用
     * @author 王庆华
    * @age 18
    * @DateTime 2017年8月31日 16:08:56
    */ public class PropertiesDemo2 { public static void main(String[] args) throws IOException { Properties pro1 = new Properties(); //Object setProperty(String key, String value):插入元素 pro1.setProperty("name", "陆离"); pro1.setProperty("age", "19"); //String getProperty(String key) :获取元素 System.out.println(pro1.getProperty("name"));//陆离 //Enumeration<?> propertyNames():获取所有键的集合 Enumeration<?> names = pro1.propertyNames(); while(names.hasMoreElements()){ System.out.println(names.nextElement());//age name } //Set<String> stringPropertyNames() :获取所有String类型的键的集合 Set<String> names2 = pro1.stringPropertyNames(); System.out.println(names2);//[age, name] //void list(PrintStream out) :将Properties对象的所有键按照字节打印流输出写入到磁盘 // PrintStream ps = new PrintStream("pro.properties"); // pro1.list(ps); // ps.close(); //void list(PrintWriter out) :使用字节流 // PrintWriter pw = new PrintWriter("pro.properties"); // pro1.list(pw); // pw.close(); // void store(OutputStream out, String comments) : 与IO结合,使用字节输出流以键值对形式持久到磁盘,第二个参数是描述..可null OutputStream out = new FileOutputStream("pro.properties"); pro1.store(out, "测试demo"); out.close(); /* * pro.properties文件内容如下: * #u6D4Bu8BD5demo * #Thu Aug 31 15:56:53 CST 2017 * age=19 * name=u9646u79BB */ // void store(Writer writer, String comments) : 与IO结合,使用字符输出流以键值对形式持久到磁盘,第二个参数是描述..可null Writer wr = new FileWriter("pro.properties"); pro1.store(wr, null); wr.close(); //void load(InputStream inStream) : 与IO结合,使用字节输入流从磁盘加载到Properties对象中 Properties pro2 = new Properties(); pro2.load(new BufferedInputStream(new FileInputStream("pro.properties"))); System.out.println(pro2); // void load(Reader reader) : 与IO结合,,使用字符输入流从磁盘加载到Properties对象中 Properties pro3 = new Properties(); pro3.load(new BufferedReader(new InputStreamReader(new FileInputStream("pro.properties")))); System.out.println(pro3); } }

    总结

      此类很常用

             

  • 相关阅读:
    第六课 使用oflash软件烧写bin文件至开发板
    Linux查看、添加、修改PATH环境变量
    第七课 Linux裸机开发+SourceInsight3.5使用+notepad++使用
    第五课 Linux高级命令
    数组的方法总结
    浅谈 return false 和preventDefault stopPropagation stopImmediatePropagation 的正确用法
    实时统计输入的文字
    滚轮滚动事件
    window.onload和DOMReady
    JS获取浏览器可视区域的尺寸
  • 原文地址:https://www.cnblogs.com/wqh17/p/7458771.html
Copyright © 2020-2023  润新知