使用Properties集合存储数据,遍历取出Properties集合中的数据与Properties集合中的方法store

使用Properties集合存储数据,遍历取出Properties集合中的数据

概述
java.util. Properties继承于Hashtable,来表示一个持久的属性集。它使用键值结构存储数据,每对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,system.getProperties方法一个Properties对象。
Properties类构造方法
  public Properties(:创建一个空的属性列表。
基本的存储方法
  public 0bject setProperty (String key,string value):保存一对属性。
  public string getProperty(String key):使用此属性列表中指定的键搜索属性值。public Set<String> stringPropertyNames()︰所有键的名称的集合。

 

Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。

package Demo_Properties;

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

/*
java.util.Properties集合extends Hashtable<k, v> implements Map<k, v>Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。Properties集合是一个唯—和i0流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储可可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用
属性列表中每个键及其对应值都是一个字符串。
Properties集合是一个双列集合, key和value黑认都是字符串
 */
public class Demo01_Properties {
    public static void main(String[] args) {
        show01();
    }

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

    private static void show01() {
        //创建 Properties集合对象
        Properties pes = new Properties();
        //使用setProperty往集合中添加数据
        pes.setProperty("马可波罗","152");
        pes.setProperty("安琪拉","106");
        pes.setProperty("鲁班大师","103");
        //pes.put(1,true);

        //使用stringPropertyNames把Properties集合中的键取出,存储到一个set集合中
        Set<String> set = pes.stringPropertyNames();

        //遍历Set集合,取出Properties  集合的每一个键
        for (String key : set){
            String value = pes.getProperty(key);
            System.out.println(key+"="+value);

        }


    }
}

 

Properties集合中的方法store

package Demo_Properties;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/*
java.util.Properties集合extends Hashtable<k, v> implements Map<k, v>Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。Properties集合是一个唯—和i0流相结合的集合
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储可可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用
属性列表中每个键及其对应值都是一个字符串。
Properties集合是一个双列集合, key和value黑认都是字符串
 */
public class Demo01_Properties {
    public static void main(String[] args) throws IOException {
        show02();
    }
        /*
        可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储void store( outputstream out,string comments )
void store (luriter writer,string comments)参数:
OutputStream out :字节输出流,不能写入中文writer writer:字符输出流,可以写中文
String comments :注释,用来解释说明保存的文件是做什么用的
不能使用中文,会产生乱码,黑认是unicode编码
一般使用""空字符串
使用步骤:
1.创建Properties集合对象,i添加数据
2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
4.释放资源
         */
    private static void show02() throws IOException {
        Properties pes = new Properties();
        //使用setProperty往集合中添加数据
        pes.setProperty("马可波罗","152");
        pes.setProperty("安琪拉","106");
        pes.setProperty("鲁班大师","103");
      /*
        //2.创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
        FileWriter fileWriter = new FileWriter("q.txt");
        //3.使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
        pes.store(fileWriter,"save data");
        //4.释放资源
        fileWriter.close();
       */
        pes.store(new FileOutputStream("q.txt"),"");

    }

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

    private static void show01() {
        //创建 Properties集合对象
        Properties pes = new Properties();
        //使用setProperty往集合中添加数据
        pes.setProperty("马可波罗","152");
        pes.setProperty("安琪拉","106");
        pes.setProperty("鲁班大师","103");
        //pes.put(1,true);

        //使用stringPropertyNames把Properties集合中的键取出,存储到一个set集合中
        Set<String> set = pes.stringPropertyNames();

        //遍历Set集合,取出Properties  集合的每一个键
        for (String key : set){
            String value = pes.getProperty(key);
            System.out.println(key+"="+value);

        }


    }
}

 

posted @ 2022-07-09 16:07  zj勇敢飞,xx永相随  阅读(30)  评论(0编辑  收藏  举报