Map接口实现类Properties【重点】

基本介绍:

  1. Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存数据
  2. 它使用特点和Hashtable类似
  3. PropertiesHashtable的子类所以键值都不可以存放null值,否则会报空指针异常
  4. Properties可以有相同的key,则原先的value值也会被替换掉
  5. Properties还可以用于从xxx.properties文件中,加载数据到Properties类对象,并进行读取和修改
  6. 说明:工作中xxx.properties文件通常作为配置文件,这个知识占在IO流举例

properties 自定义参数_属性列表

Properties继承关系

properties 自定义参数_java_02

常用方法:

增加
1.   put  增加

删除
2.   remove  删除

查
3.   get    查【也是获取对应的value值】
3.   getProperty  查 根据传入的字符串,获取相应的value值

修改
4.   put  当传入的key相同时,就相当于修改了

Modifier and Type

Method and Description

String

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

String

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

void

list(PrintStream out) 将此属性列表打印到指定的输出流。

void

list(PrintWriter out) 将此属性列表打印到指定的输出流。

void

load(InputStream inStream) 从输入字节流读取属性列表(键和元素对)。

void

load(Reader reader) 以简单的线性格式从输入字符流读取属性列表(关键字和元素对)。

void

loadFromXML(InputStream in) 将指定输入流中的XML文档表示的所有属性加载到此属性表中。

Enumeration<?>

propertyNames() 返回此属性列表中所有键的枚举,包括默认属性列表中的不同键,如果尚未从主属性列表中找到相同名称的键。

Object

setProperty(String key, String value) 致电 Hashtable方法 put

void

store(OutputStream out, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法加载到 Properties表中的格式输出流。

void

store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式输出到输出字符流。

void

storeToXML(OutputStream os, String comment) 发出表示此表中包含的所有属性的XML文档。

void

storeToXML(OutputStream os, String comment, String encoding) 使用指定的编码发出表示此表中包含的所有属性的XML文档。

Set<String>

stringPropertyNames() 返回此属性列表中的一组键,其中键及其对应的值为字符串,包括默认属性列表中的不同键,如果尚未从主属性列表中找到相同名称的键。

Properties常用方法

package collection_.collectionP.list_.hashtable_;

import java.util.Properties;

/**
 * @author: 海康
 * @version: 1.0
 */
public class Properties01 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        // 注意是Properties 键和值 都不能存放 null 值
        // 常用方法如下 :
        // put 添加
        properties.put(101,"湛江");
        properties.put(102,"海康");
        properties.put("103","广州");

        // remove 删除
//        properties.remove(101);

        // put 修改 当传入相同的key值时,就相当于修改 value 值
        properties.put(102,"南宁");

        // get 查
        properties.get(101);
        System.out.println(properties.getProperty("103"));
    }
}