Use Properties Configure the profile to read

# Use Properties Configure the profile to read:

For example: the content of a configuration file as follows:

# setting.properties

last_open_file=/data/hello.txt
auto_save_interval=60

The .properties file can be read from the file system:

String f = "setting.properties";
Properties props = new Properties();
props.load(new java.io.FileInputStream(f));

String filepath = props.getProperty("last_open_file");
String interval = props.getProperty("auto_save_interval", "120");

Properties generally used to read configuration files, a total of three steps:

    * 创建Properties实例;   
    * 调用load()读取文件;
    * 调用getProperty()获取配置。

Classpath .properties can be read from a file, because Load (InputStream) method receives an InputStream instance, represents a stream of bytes, it is not necessarily the file stream, the stream may be read from a resource jar package:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/common/setting.properties"));

If you have multiple .properties files, you can repeatedly call load () reads, key-value key-value will override the read after read:

Properties props = new Properties();
props.load(getClass().getResourceAsString("/common/setting.properties"));
props.load(new FileInputStream("C:\\conf\\setting.properties"));

The above code demonstrates a common usage Properties: You can change the default configuration file in the classpath, then write another profile based on the machine's environment, covering some of the default configuration.

The purpose is to store design Properties of type String key-value, but Hashtable Properties fact derived from its design actually is a problem, but in order to maintain compatibility, now not changed. In addition to getProperty () and setProperty () method, as well as inherited from Hashtable down get () and put () method, parameter signature of these methods is Object, when we use Properties, do not go calling them inherited from Hashtable Methods.


Write configuration file:

Modification If the setProperty () the Properties instance, you can write to the configuration file, get the latest configuration so that when the next start. Use the configuration file 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"), "这是写入的properties注释");

Since the load (InputStream) always default to the code reading ASCII byte stream, it will cause distortion to read. We need to read using another overloaded method load (Reader):

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

It can read Chinese normal. InputStream and Reader is the difference between a byte stream, it is a stream of characters. Character stream in memory have been represented as a char, it does not involve coding problem.

Guess you like

Origin www.cnblogs.com/chenyameng/p/11441405.html