Properties -IO correlated double row collections

IO-related collections


  • java.util.Properties collection extends hashtable (eliminated)
  • Properties class represents a persistent set of properties. Properties can be saved or loaded stream from the stream
  • Properties set is a unique combination of flow and a set of IO

    1. The method may be used to store Properties set, the temporary data set, is written into persistent storage drives
    2. The method may be used in load Properties set to save the file in a hard disk (key-value pairs), using the set of read
  • Each key in the list and the corresponding value of the attribute is a string.
    1. Properties collection is a set of double row, note that the generic default value is a string key and
    2. Object setProperty (String key, String value) calls the Hashtable method put.
    3. String getProperty (String key) to find the value value by key, this method is equivalent Map collection get (key) method
    4. Set stringPropertyNames () Returns a property list a set of keys, wherein the key and its corresponding value is a string, the same name if the key is not found in the list of the main attributes, the default property list further includes a different key.
    private static void show01() {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();
        //使用setProperties();添加数据
        properties.setProperty("No.1","王小帅");
        properties.setProperty("No.2","王一帅");
        properties.setProperty("No.3","王二帅");

        //使用stringPropertyNames把Properties集合中的key取出放入set集合
        Set<String> set = properties.stringPropertyNames();

        //遍历set集合,取出Properties集合的每一个键
        for (String key:set) {
            //通过getProperty方法通过key获取value
            String value = properties.getProperty(key);
            System.out.println(key + ":"+value);
        }

    }

store method


  • void store(OutputStream out,String comments)
  • void store(Writer writer,String comments)
  • parameter:
    1. OutputStream out, output stream of bytes, and write Chinese
    2. Writer writer: character-output stream, you can write Chinese
    3. String comments: an explanation for the saved file is what to do, not Chinese, will be garbled, the default Unicode encoding. General comments to the empty string
  • Steps for usage
    1. Creating Properties collection object, add data
    2. Create byte / character output stream object constructor bound to lose ground
    3. Use store Properties collection, the data collection time, written to the hard disk storage persisted
    4. Release resources
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class Propertiess {
    public static void main(String[] args) {
        try {
            show01();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void show01() throws IOException {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();
        //使用setProperties();添加数据
        properties.setProperty("No.1","王小帅");
        properties.setProperty("No.2","王一帅");
        properties.setProperty("No.3","王二帅");

        //使用字符输出流,绑定输出目的地
        FileWriter writer = new FileWriter("b.txt");

        //使用Properties的方法store,把集合中的临时数据,持久化写入文件中
        properties.store(writer,"");
        /*
        # -comments注释类容
        #Thu Sep 26 18:21:37 CST 2019
        No.2=王一帅
        No.1=王小帅
        No.3=王二帅
         */

        //使用字节输出流
        properties.store(new FileOutputStream("a.txt"),"error");
        /*
        #error
        #Thu Sep 26 18:27:58 CST 2019
        No.2=\u738B\u4E00\u5E05
        No.1=\u738B\u5C0F\u5E05
        No.3=\u738B\u4E8C\u5E05
         */

        //关闭流
        writer.close();

    }

}

load method

  • Properties of objects can be key to read into memory
  • void load(InputStream inStream)
  • void load (Reader reader), using the above method
  • Difference: No comments parameter
  • Note: file
    1. Reading a file key for a connector may be a = [,] and the other symbols spaces
    2. # Beginning of a comment using key-value pairs can not be read
    3. The default read a string, do not deliberately quoted in the text
package cn.learn.properties;


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

public class Propertiess {
    public static void main(String[] args) {
        try {
            show01();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void show01() throws IOException {
        //创建Properties对象,默认字符串
        Properties properties = new Properties();

        //使用字符输入流,绑定读取目的地
        FileReader reader = new FileReader("b.txt");

        //读取键值对进入集合,若有#开头的串不会被读取
        properties.load(reader);

        //遍历查看
        Set<String> strings = properties.stringPropertyNames();
        for (String key:strings) {
            System.out.println(key+":"+properties.getProperty(key));
            /*
            No.2:王一帅
            No.1:王小帅
            No.3:王二帅
             */
        }
        
        //释放
        reader.close();
    }

}

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11593666.html
Row