Basic operation and introduction of [Java foundation] Properties class

Keywords: Java JavaSE properties

introduction

The. properties file in Java is a configuration file, which is mainly used to express configuration information; Generally speaking, the stored data is like the corresponding relationship between key and value in Map; In this way, attributes can be matched through key value pairs, and each key and its corresponding value in the attribute list are a String (String type);

In Java, there is a Properties class that can be used to save property sets, which can load property sets from the stream; So next, I will introduce in detail how to use the Properties class and its common methods;

Basic introduction to Properties class and common methods

Properties:

chinese:
The properties class represents a persistent set of properties. Properties can be saved in or loaded from a stream. Each key and its corresponding value in the attribute list is a string.
An attribute list can contain another attribute list as its "default value"; If the attribute key cannot be searched in the original attribute list, the second attribute list is searched.


english:
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its "defaults"; this second property list is searched if the property key is not found in the original property list.

Here are a few common methods, which I will use in the next operation;

1,String getProperty(String key)

Introduction: get value through key

2,void load(InputStream inStream/Reader reader)

Introduction: read the contents of the file into memory

3,Object setProperty(String key, String value)

Introduction: set (add) new key value pairs

4,void store(OutputStream out / Writer writer, String comments)

Introduction: write the corresponding contents in memory to the file

These methods are commonly used. Of course, several methods are not written. If necessary, you can query them in the API document;

Next, I will implement some operations with code. Here, first put out the. properties file:
The following code operations are based on this file;

read operation

The read operation can read the file through the load method, and then obtain the value value through getProperty;
Let me use a code example:

package io;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

// Read the. properties file
public class PropertiesBlog01 {
    // Read in. properties file
    public void readProperties() throws IOException {
        Properties properties = new Properties();
        // Read the data of the file into memory
        properties.load(new FileInputStream("blogtest.properties"));
        // Contents of output file
        System.out.println(properties.getProperty("name")); // Get name
        System.out.println(properties.getProperty("age")); // Get age
        System.out.println(properties.getProperty("gender")); // Get gender
    }

    // main method call test
    public static void main(String[] args) throws IOException {
        PropertiesBlog01 propertiesBlog01 = new PropertiesBlog01();
        propertiesBlog01.readProperties();
    }
}

Output results:

jack
18
man

Write operation

The write operation is interesting. There are three types of writing:

  1. Writing new data overwrites the previous data
  2. Writing new data will not overwrite the previous data, but if there are duplicate key values, they will not be overwritten
  3. Writing new data will not overwrite the previous data, and duplicate key values will be overwritten

These three situations are generally used according to needs, but I think the third one is most commonly used (just personal point of view);
Let me demonstrate with the code, which has detailed comments;

package io;

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

// Implement writing to the. properties file
public class PropertiesBlog02 {
    // Write. properties file
    public void writeProperties() throws IOException {
        Properties properties = new Properties();
        // Here are three write cases

        // 1. Write new data to overwrite the previous data
        // Add content to setting file
        properties.setProperty("IdNum", "123456789");
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
        // Output results:
        // Original file content: write new data and overwrite as follows:
        // name=jack         #this is a comment
        // age=18            #Fri Nov 19 19:58:08 CST 2021
        // gender=man        IdNum=123456789

        // 2. Writing new data will not overwrite the previous data, but if there are duplicate key values, they will not be overwritten
        // Add content to setting file
        properties.setProperty("IdNum", "repeat");
        properties.store(new FileOutputStream("blogtest.properties", true), "this is a comment");
        // Output results:
        // Original file content: after new data is written, it is:
        // #this is a comment                #this is a comment
        // #Fri Nov 19 19:58:08 CST 2021     #Fri Nov 19 20:06:03 CST 2021
        // IdNum=123456789                   IdNum=123456789
        //                                   #this is a comment
        //                                   #Fri Nov 19 20:06:03 CST 2021
        //                                   IdNum=repeat

        // 3. Writing new data will not overwrite the previous data, and duplicate key values will be overwritten
        properties.load(new FileInputStream("blogtest.properties"));
        properties.setProperty("IdNum", "newIdNum");
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
        // Output results:
        // Original file content: after new data is written, it is:
        // #this is a comment                #this is a comment
        // #Fri Nov 19 20:06:03 CST 2021     #Fri Nov 19 20:13:22 CST 2021
        // IdNum=123456789                   IdNum=newIdNum
        // #this is a comment
        // #Fri Nov 19 20:06:03 CST 2021
        // IdNum=repeat
    }

    public static void main(String[] args) throws IOException {
        PropertiesBlog02 propertiesBlog02 = new PropertiesBlog02();
        propertiesBlog02.writeProperties();
    }
}

If you don't understand it, just remember it. If you forget it, just look at it. If you write too much, you may understand it one day;

Delete operation

The remove method called by this operation is interesting because you can't find the Properties class in the API document. If you have this method, you can see the source code:

It can be found that it actually calls someone else's remove method, so whose is it? Let's look further:

This is actually the method of ConcurrentHashMap, so the remove method is not recorded in the Properties class;

In fact, just learn how to use it. It's also very simple to use. Similarly, use code to demonstrate:

package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

// Delete the contents of the. properties file
public class PropertiesBlog03 {
    public void deleteProperties() throws IOException {
        Properties properties = new Properties();
        // Load file data into memory
        properties.load(new FileInputStream("blogtest.properties"));
        // Delete corresponding data through key value
        properties.remove("gender"); // Delete gender
        //Reset data in file
        properties.store(new FileOutputStream("blogtest.properties"), "this is a comment");
    }

    public static void main(String[] args) throws IOException {
        PropertiesBlog03 propertiesBlog03 = new PropertiesBlog03();
        propertiesBlog03.deleteProperties();
    }
}

After running, the file becomes:

#this is a comment
#Fri Nov 19 21:22:00 CST 2021
name=jack
age=18

You can see that the gender key value pair has been deleted;

summary

These are some basic introductions to Properties. You may encounter other problems when using them. You can communicate with me in the comment area. I hope this article can help you!!

Welcome your comments!

Posted by snowdog on Sun, 21 Nov 2021 01:03:38 -0800