[Java foundation] using Properties

Article directory

1, The. properties file in Java

When writing Java programs, you often need to read and write. properties configuration files. as

# Last opened file:
last_open_file=/data/hello.txt
# Auto save files every:
auto_save_interval=60
  • The feature of. properties file is that its key value is generally of string string type, so it can be represented by map < string, string >.

  • The Java collection library provides a Properties class to represent a set of configurations. Due to the historical legacy, Properties is essentially a Hashtable, but we only need to use the Properties' own interface for read-write configuration.

  • Java default configuration file takes. properties as extension, each line is represented by key=value, and comments begin with ා

2, Read profile

1. Load properties from the file stream

There are the following profiles
java last_open_file=/data/hello.txt auto_save_interval=60
There are three steps to read the configuration file with Properties:
1. Create a Properties instance;
2. Call load() to transfer in the file stream to read the file;
3. Call getProperty() to get the configuration according to the key name.

```java
String file = "setting.properties";
Properties props = new Properties();
props.load(new FileInputStream(file ));

String filepath = props.getProperty("last_open_file");
String interval = props.getProperty("auto_save_interval", "120");
```
>`call getProperty()`When getting the configuration,`If key Does not exist, will return null`. Sure`Set default`,When key If it does not exist, the default value is returned.

2. Load configuration file from classPath

- from`classpath`read`.properties`File because`load(InputStream)Method`Receive one`InputStream Example`,Represents a byte stream, which is not necessarily a file stream,`Or from jar Resource stream read in package:`
```java
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/common/setting.properties"));
```

3. Read a byte stream from memory to load the configuration file

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

public class Main {
	public static void main(String[] args) throws IOException {
    	String settings = "# test" + "\n" + "course=Java" + "\n" + "last_open_date=2019-08-07T12:35:01";
    	ByteArrayInputStream input = new ByteArrayInputStream(settings.getBytes("UTF-8"));
    	Properties props = new Properties();
    	props.load(input);

    	System.out.println("course: " + props.getProperty("course"));
    	System.out.println("last_open_date: " + props.getProperty("last_open_date"));
    	System.out.println("last_open_file: " + props.getProperty("last_open_file"));
    	System.out.println("auto_save: " + props.getProperty("auto_save", "60"));
	}
}
```

results of enforcement

4. Precautions

  1. The purpose of the Properties design is to store the key value key value pairs of String type, but the Properties are actually derived from the Hashtable. Its design is actually problematic, but in order to maintain compatibility, it can't be modified now.
  2. In addition to getProperty() and setProperty() methods, there are also get() and put() methods inherited from Hashtable. The parameter signature of these methods is Object. When using Properties, we should not call these methods inherited from Hashtable.

3, Write profile

If you can modify the Properties instance through setProperty(), write the configuration to a file so that you can get the latest configuration the next time you start.

Write configuration file using store() method

Properties props = new Properties();
props.setProperty("url", "http://www.liaoxuefeng.com");
props.setProperty("language", "Java");
props.store(new FileOutputStream("C:\\conf\\setting.properties"), "This is written properties Notes");

Four. Coding

  • In previous versions of Java, the. properties file encoding was ASCII (ISO8859-1). If Chinese is involved, it must be represented by name=\u4e2d\u6587. Starting with JDK9, Java's. properties file can be encoded using UTF-8.

  • It should be noted that since load(InputStream) always reads byte stream in ASCII code by default, it will cause reading garbled code. We need to use another overload method load(Reader reader,String charset) to read:

    Properties props = new Properties();
    props.load(new FileReader("settings.properties", StandardCharsets.UTF_8));
    

The difference between InputStream and Reader is that one is byte stream and the other is character stream. Character stream has been represented by char type in memory, and it does not involve encoding.

Five. Conclusion

  1. properties provided by Java collection library are used to read and write configuration file. properties. The. properties file can be encoded using UTF-8.

  2. You can read the. properties file from the file system, classpath, or anywhere else.

  3. When reading and writing Properties, only use getProperty() and setProperty() methods, and do not call inherited methods such as get() and put().

Published 69 original articles, won praise 116, visited 6339
Private letter follow

Tags: Java encoding ascii

Posted on Fri, 31 Jan 2020 22:54:57 -0800 by bbauder