properties

一,概述:

  1. 是一个 Map体系的集合
  2. properties中又跟IO相关的方法
  3. 键值对的数据类型基本定义为字符串                

二,properties中特有的方法:

  1. Setproperties()
  2. getProperties()
  3. StringPropertyNames()获取所有键值集

 

 

三,properties结合IO的方法

                               

方法名 说明
void load(inputstream is) 从输入字节流读取属性列表(键和元素对)
void load(Reader reader) 从输入字符流读取属性列表(键和元素对)
void store (Outputstream out,String comments) 将此层属性列表(键和元素对)写入此properties表中,以适合与使用load(inputStream)方法的格式写入输出字节流
void store (Writer writer,String comments) 将此层属性列表(键和元素对)写入此properties表中,以适合与使用load(Reader)方法的格式写入输出字符流

 

 

 

 

package com.guancun.fileoutputstream.properties;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
    public static void main(String[] args) {
        Properties prop = new Properties();
        //增
        prop.put("地址","001");
        prop.put("数据源","c3p0");
        System.out.println(prop);
        //删除
        prop.remove("地址");
        System.out.println(prop);
        //改
        prop.put("地址","002");
        System.out.println(prop);
        //查
        System.out.println(prop.get("数据源"));
        //便利keySet获取所有键
        Set<Object> objects = prop.keySet();
        for (Object object : objects) {
            System.out.println("keyset:  "+object+"--"+prop.get(object));
        }
        //entrySet获取键值对对象
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println("entrySet: "+entry.getKey()+"--"+entry.getValue());

        }
    }
}
package com.guancun.fileoutputstream.properties;


import java.util.Properties;
import java.util.Set;
//Properties特有的三个方法
public class PropertiesTest2 {
    public static void main(String[] args) {

        Properties prop = new Properties();
        //增
        prop.setProperty("地址","001");
        prop.setProperty("数据源","c3p0");
        System.out.println(prop);
        //查
        System.out.println(prop.getProperty("数据源"));
        //获取所有键集
        Set<String> set = prop.stringPropertyNames();

        for (Object object : set) {
            System.out.println("stringPropertyNames:  "+object+"--"+prop.get(object));
        }

    }
}
package com.guancun.fileoutputstream.properties;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class LoadAndStore {
    public static void main(String[] args) throws IOException {
        //method1();写
        //load将本地文件中的键值对读取到集合中
        Properties properties = new Properties();
        FileReader fileReader = new FileReader("prop.properties");
        properties.load(fileReader);
        fileReader.close();
        System.out.println(properties);

    }

    private static void method1() throws IOException {
        //将集合中的数据以键值对的形式存到本地文件中
        Properties properties = new Properties();
        properties.setProperty("name","骚林");
        properties.setProperty("address","001");
        properties.setProperty("resoces","c3p0");
        FileWriter fileWriter = new FileWriter("prop.properties");
        properties.store(fileWriter,"");
        fileWriter.close();
    }
}

 

上一篇:JSON.stringify 函数与 Object.defineProperty 的坑:不可枚举属性无法写入


下一篇:开发岗位面试你应该知道的回答技巧!