35Propiedades<Colección>

Puntos de conocimiento: 

La clase Java.util.Properties hereda de Hashtable para representar un conjunto persistente de propiedades. Almacena datos utilizando una estructura clave-valor, cada clave y su valor correspondiente es una cadena, la clase

La clase de propiedades representa un conjunto persistente de propiedades que se pueden guardar o cargar desde una secuencia. Cada clave y su contraparte en la lista de propiedades es una cadena.

La colección de propiedades es una colección que se combina únicamente con el flujo de E/S.
Puede usar el método store() en la colección de propiedades para escribir de forma persistente los datos temporales de la colección en el disco duro para guardarlos.
Puede usar el método load( ) en la colección de propiedades para almacenar los datos temporales de la colección. El archivo (par clave-valor) guardado en el disco duro se lee en la colección para su uso.

 Cada clave y su contraparte en la lista de propiedades es una cadena.
  La colección de propiedades es una colección de dos columnas y la clave y el valor son cadenas de forma predeterminada.

Código:

package demo36Properties集合;

import java.io.*;
import java.util.Properties;
import java.util.Set;

/*
java.util.Properperties集合 -继承于Hashtable<k,v> -继承Map<k,v>
Properties类表示一个持久的属性集,Properties可保存在流中或从流中加载。
Properties集合是一个唯一和IO流相结合的集合
可以使用Properties集合中的方法store(),把集合中的临时数据,持久化写入到硬盘中保存
可以使用Properties集合中的方法load(),把硬盘中保存的文件(键值对),读取到集合中使用

属性列表中每个键及其对应都是一个字符串。
  Properties集合是一个双列集合,key和value默认是字符串


 */
public class DenoProperties {
    public static void main(String[] args) throws IOException {
        //show01();
       // show02();
        show03();
    }

    private static void show03() throws IOException {
        /*
        可以使用Properties集合中的方法load(),把硬盘中保存的文件(键值对),读取到集合中使用
        load(InputStream inStream)
        load(Reader reader)
        参数:
          InputStream inStream:字节输入流,不能读取含有中文的键值对
          Reader reader:字符输入流,能读取含有中文键值对
          使用步骤:
          1.创建Properties集合对象
          2.使用Properties集合对象中的方法load读取保存键值对文件
          3.遍历Properties集合
          注意:
          1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
          2.存储键值对的文件中,可以使用#进行注释,该注释的键值对不会再被读取
          3.存储键值对的文件中,键与值默认都是字符串,不用在加引号
         */
        
        // 1.创建Properties集合对象
        Properties prop=new Properties();
        // 2.使用Properties集合对象中的方法load读取保存键值对文件
        prop.load(new FileReader("E:\\多线程\\prop.txt"));
        //prop.load(new FileInputStream("E:\\多线程\\prop.txt"));//不能读取中文的文件,否则会乱码
        //3.遍历Properties集合
        Set<String> set=prop.stringPropertyNames();使用stringPropertyNames()方法把Properties集合中的键取出来,存储到一个Set集合中
        for (String key: set) {//增强for循环
            String value=prop.getProperty(key);
            System.out.println(key+"="+value);
        }

    }

    private static void show02() throws IOException {
         /*
    可以使用Properties集合中的方法store(),本集合中的临时数据,持久化写入到硬盘中保存
      store(OutputStream out,String comments)
      store(writer writer,String comments)
      参数:
         OutputStream out:字节输出流,不能写入中文
         writer writer:字符输出流可以写中文
         String comments:注释,解释说明保存的文件是做什么用的,不能使用中文会产生乱码,默认Unicode编码
         一般使用空字符串

         使用步骤:
         1.创建Properties集合对象,添加数据
         2.创建字节输出流/字符输出流,构造方法中绑定要输出的目的地
         3.使用Properties集合中的方法store(),本集合中的临时数据,持久化写入到硬盘中保存
         4.释放资源
    */

        //1.创建Properties集合对象
        Properties prop=new Properties();
        //使用setProperties方法往集合中添加数据
        prop.setProperty("赵丽颖","168");
        prop.setProperty("迪丽热巴","168");
        prop.setProperty("古力娜扎","168");

        //2.创建字节输出流/字符输出流,构造方法中绑定要输出的目的地
        FileWriter fw=new FileWriter("E:\\多线程\\prop.txt");
        //3.使用Properties集合中的方法store(),把集合中的临时数据,持久化写入到硬盘中保存
        prop.store(fw,"savedata");
        // 4.释放资源
        fw.close();
        //字节流不能写中文,会乱码,匿名流不用关闭
        prop.store(new FileOutputStream("E:\\多线程\\prop2.txt"),"");
    }


    private static void show01() {
         /*
    使用Properties集合存储数据,遍历取出Properties集合中的数据
    Properties集合有一些操作字符串的特有方法:
      setProperties(String key,String value)  调用Hashtable的方法 put()方法
      getProperties(String key) 用指定的键值在此属性列表中搜索属性。 通过key知道value,此方法相当Map集合的get()方法
      stringPropertyNames()返回此属性列表中的键集,其中该键值及其对应的值是字符串   此方法相当Map集合的keySet()方法
     */
        //创建Properties集合对象
        Properties prop=new Properties();
        //使用setProperties方法往集合中添加数据
        prop.setProperty("赵丽颖","168");
        prop.setProperty("迪丽热巴","168");
        prop.setProperty("古力娜扎","168");

        //使用stringPropertyNames()方法把Properties集合中的键取出来,存储到一个Set集合中
        Set<String> set=prop.stringPropertyNames();
        //遍历Set集合,取出Properties的每一个键
        for (String key: set) {//增强for遍历
            //使用getProperties()方法通过key获取value
                String value=prop.getProperty(key);
            System.out.println(key+"="+value);
        }


    }
}

 Ejecute el método show01:

Ejecute el método show02:

 

 

 Ejecute el método show03:

 

Supongo que te gusta

Origin blog.csdn.net/dengfengling999/article/details/123997673
Recomendado
Clasificación