当前位置:网站首页>Understanding and learning of properties class and properties configuration file
Understanding and learning of properties class and properties configuration file
2022-07-27 07:33:00 【Daxiong who loves learning】
List of articles
introduction
Maybe our java While the program is running , We need to modify one of the configured values , But it is impossible for us to modify the value after stopping the program , This is the time , It reflects the use properties The benefits of configuration files .
If the value is properties In the configuration file , It can follow very well and conveniently java Programs interact , We don't need to stop the program , Can be modified directly properties Values in the configuration file , that java This value in the program can be changed .
and Properties class , yes java Read from the program properties Preferred class of configuration file , So we need to master it !
Basic introduction
The parent class of this class is HashTable, So is the underlying principle HashTable

This class is specially used for reading and writing properties Collection class of configuration file , The configuration file format is as follows :
key = value
key = value
** Be careful :** Key value pairs do not need spaces , Values do not need to be enclosed in quotation marks , The default type is String
Common methods
load: Load the key value pair of the configuration file to Properties objectlist: Display data to the specified devicegetProperty(key): Get value from keysetProperty(key,value): Set key value pair to Properties objectstore: take Properties The key value pairs in are stored in the configuration file , stay idea Save information to the configuration file , If it contains Chinese , Will be stored as unicode code
Requirements coding
demand 1: Read profile content
The following configuration file mysql.properties
ip=0.0.0.0
user=root
pwd=root
stay java Read its value in the program
The traditional way to achieve
Use character stream to read
/** * @author: Lei Zijie * @date:2022/7/27 */
public class Properties01 {
public static void main(String[] args) throws IOException {
// Get the current working directory , Back file The relative path of is written according to this
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
File file = new File("basic_review\\src\\properties_\\mysql.properties");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while((line = bufferedReader.readLine())!=null){
// Split the read string
String[] split = line.split("=");
System.out.println(split[0]+" The value is :"+split[1]);
// The following is the output of all the contents
//System.out.println(line);
}
bufferedReader.close();
}
}

Use Properties Class read
Use Properties Class to read the contents of the configuration file
/** * * Use Properties To read the file * * @author: Lei Zijie * @date:2022/7/27 */
public class Properties02 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
// Get the current working directory , Back file The relative path of is written according to this
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
// Loads the specified configuration file , The relative path is the relative path of the current working directory
properties.load(new FileReader("basic_review\\src\\properties_\\mysql.properties"));
// take k-v Display console
properties.list(System.out);
System.out.println("=====================");
// according to key Get the value for
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println(" user name :"+user);
System.out.println(" password :"+pwd);
}
}

demand 2: Add profile content
take Properties The key value pairs created by the class are written to a new file
/** * * Use properties Class to create a configuration file * * @author: Lei Zijie * @date:2022/7/27 */
public class Properties03 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
// establish
properties.setProperty("user","lzj");// If the user name is Chinese , Will save unicode code
properties.setProperty("pwd","123456");
properties.setProperty("age","22");
// Store in file
// In formal parameter comments by null that will do , If you add, you are adding comments
properties.store(new FileOutputStream("basic_review\\src\\properties_\\mysql2.properties"),null);
System.out.println(" Successfully saved the configuration ");
}
}

demand 3: Modify the read configuration file
Read first , Then modify the configuration and write it to the original file
/** * * Use Properties To modify the read file * * @author: Lei Zijie * @date:2022/7/27 */
public class Properties04 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
// Get the current working directory , Back file The relative path of is written according to this
//System.out.println(System.getProperty("user.dir"));//E:\Allworkspaces\idea-workspace\JavaEE
// Loads the specified configuration file , The relative path is the relative path of the current working directory
properties.load(new FileReader("basic_review\\src\\properties_\\mysql.properties"));
// take k-v Display console
properties.list(System.out);
// modify user
properties.setProperty("user","lzj");
System.out.println("=====================");
// according to key Get the value for
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println(" user name :"+user);
System.out.println(" password :"+pwd);
// Write the modified key value pair into the file
properties.store(new FileOutputStream("basic_review\\src\\properties_\\mysql.properties"),null);
}
}

All the way
| Modifier and Type | Method and Description |
|---|---|
String | getProperty(String key) Use the key specified in this property list to search for properties . |
String | getProperty(String key, String defaultValue) Use the key specified in this property list to search for properties . |
void | list(PrintStream out) Print this property list to the specified output stream . |
void | list(PrintWriter out) Print this property list to the specified output stream . |
void | load(InputStream inStream) Read the attribute list from the input byte stream ( Bond and element pair ). |
void | load(Reader reader) Read the attribute list from the input character stream in a simple linear format ( Keywords and element pairs ). |
void | loadFromXML(InputStream in) The... In the input stream will be specified XML All properties represented by the document are loaded into this property sheet . |
Enumeration<?> | propertyNames() Returns an enumeration of all keys in this property list , Include different keys in the default attribute list , If a key with the same name has not been found in the main attribute list . |
void | save(OutputStream out, String comments) Have been abandoned If... Occurs while saving the attribute list I / O error , This method will not throw IOException. Save the property list store(OutputStream out, String comments) The way is through store(OutputStream out, String comments) Method or storeToXML(OutputStream os, String comment) Method . |
Object | setProperty(String key, String value) Call Hashtable Method put . |
void | store(OutputStream out, String comments) Add this property to the list ( Bond and element pair ) Write this Properties In the table , To be suitable for use load(InputStream) Method is loaded to Properties Format output stream in table . |
void | store(Writer writer, String comments) Add this property to the list ( Bond and element pair ) Write this Properties In the table , To be fit for use load(Reader) Method to the output character stream . |
void | storeToXML(OutputStream os, String comment) Issue a that represents all the attributes contained in this table XML file . |
void | storeToXML(OutputStream os, String comment, String encoding) Issue a that represents all attributes contained in this table using the specified encoding XML file . |
Set<String> | stringPropertyNames() Returns a set of keys in this property list , Where the key and its corresponding value are strings , Include different keys in the default attribute list , If a key with the same name has not been found in the main attribute list . |
边栏推荐
- 连接MySQL时报错:Public Key Retrieval is not allowed 【解决方法】
- Routing between VLANs (explanation + verification)
- (2022杭电多校三)1009.Package Delivery(贪心)
- flink中维表Join几种常见方式总结
- VLAN trunk experiment
- js正则表达式实现每三位数字加一个逗号
- ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
- The solution of using sqlplus to display Chinese as garbled code
- (2022牛客多校三)J-Journey(dijkstra)
- 【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
猜你喜欢

杂谈:跟女儿聊为啥要学好文化课

Grayog log server single node deployment

Expose Prometheus metrics in Perl programs

Use reflection to dynamically modify annotation attributes of @excel

2022 0726 Gu Yujia's study notes

VLAN trunk实验

Quickly update the information in a field in kettle

在Perl程序中暴露Prometheus指标

Graylog 日志服务器单节点部署

ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
随机推荐
ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
C语言程序设计 | 程序编译与预处理
在rhel7.3中编译和使用log4cxx
用shell来计算文本中的数字之和
UUID与secrets模块
一个优先级顺序的SQL问题
The solution of using sqlplus to display Chinese as garbled code
MySQL2
【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
Expose Prometheus metrics in Perl programs
在mysql中同时使用left join on 和where 的查询结果分析
JS make a traffic light
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
Graylog 日志服务器单节点部署
Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query
C winfrom common function integration-2
Functools module
docker安装MySQL8.0.28
Basic functions and collections of guava
Panabit SNMP配置