JAVA single row diary -2020 / 1/27-Properties set

1 Overview

  1. PropertiesIs a Hashtable<k,v>subclass Hashtable<k,v>is a Map<k,v>class that implements the interface
Properties extends Hashtable<k.v> implements Map<k,v>
  1. PropertiesClass represents a persistent set of properties. PropertiesCan be saved or loaded from the stream in the stream.
     ● Propertiesset is a unique combination of flow and a set of IO
     ● store()method, the temporary data set, writes to the hard disk persistence
     ● load()method, the file stored in the hard disk (key-value pairs), using the set of read
  2. Properties Collection is a double-row set, the default values ​​and keys are strings

2. Common exclusive method

1. Add data

对象.setProperty(String key,String value)

2. Locate the key values

对象.getProperty(String key)

3. The Propertiesset key out and put into a setset of

Set<String> set = 对象.stringPropertyNames();

3. storeMethods

store(OutputStream out,String comments)
store(Writer writer,String comments)

OutputStream out output byte stream can not be written Chinese
Writer writer output character stream can be written Chinese
String comments annotated to explain the saved file is used to do not use the Chinese, generally used ""(the empty string)

  • Steps for usage
  1. Creating Propertiesa collection of objects, add data
  2. Create byte / character output stream object constructor output destination bound
  3. Using the store()method, the temporary data set, writes to the hard disk persistent
  4. Release resources
package Demo01;

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

public class Demo01 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        FileWriter writer = new FileWriter("G:\\Java\\测试文件夹\\a.txt");

        properties.setProperty("1","张三");
        properties.setProperty("2","李四");
        properties.setProperty("3","王五");
        properties.setProperty("4","赵六");

        properties.store(writer,"save data");

        writer.close();
    }
}

Here Insert Picture Description

4. loadMethod

load(InputStream out);
load(Reader reader);

OutputStream out input stream of bytes,Can notRead containingChineseThe key to
the input character stream Reader reader,canRead containingChineseKey-value pairs

  • Steps for usage
  1. Creating Propertiesa collection of objects
  2. Using the load()method, the key to read the file
  3. Traversal Propertiescollection
  • note
  • The file key, the key value for the default = connectors, spaces, or other
  • Key-value pair file, you can use the # comment, the comment can not be read later
  • Key-value pair file, the default key and value are strings
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();

        properties.load(new FileReader("G:\\Java\\测试文件夹\\b.txt"));

        for (String i:properties.stringPropertyNames()){
            System.out.println(properties.get(i));
        }
    }
}

Here Insert Picture DescriptionHere Insert Picture Description

Published 103 original articles · won praise 1 · views 2645

Guess you like

Origin blog.csdn.net/wangzilong1995/article/details/104091403