当前位置:网站首页>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 . |
边栏推荐
- Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
- UUID与secrets模块
- 12. Integer to Roman
- ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
- C语言 pthread_cleanup_push()和pthread_cleanup_pop()函数(用于临界资源程序段中发生终止动作后的资源清理任务,以免造成死锁,临界区资源一般上锁)
- Panabit SNMP配置
- (2022 Niuke multi school III) j-journey (Dijkstra)
- UUID and secrets module
- Oracle cleans up the Database disk space of tables with referenced partitions
- Will Flink CDC constantly occupy Oracle's memory by extracting Oracle's data, and finally cause oracle-040
猜你喜欢

2022 0726 Gu Yujia's study notes

mysql备份策略

JS make a traffic light

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

VLAN trunk experiment

Single arm routing (explanation + experiment)

Using loops to process data in tables in kettle

Tcp/ip protocol analysis (tcp/ip three handshakes & four waves + OSI & TCP / IP model)

C common function integration-3

C语言实现猜数字小游戏项目实战(基于srand函数、rand函数,Switch语句、while循环、if条件判据等)
随机推荐
Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation
用oracle来演示外键的使用
杂谈:跟女儿聊为啥要学好文化课
在kettle使用循环来处理表中的数据
SQLite 常用功能整合
C common function integration-3
docker安装MySQL8.0.28
Bash: create a function that returns a Boolean value
Haikang H9 camera cannot be connected with xshell (SSH is not enabled)
C# 常用功能整合-2
网络入门——vlan及trunk概述
Federal Reserve SR 11-7: Guidance on model risk management - Wanzi collection
js正则表达式实现每三位数字加一个逗号
drawImage方法第一次调用不显示图片的解决方式
when的多条件查询
Using docker in MAC to build Oracle database server
Quickly update the information in a field in kettle
ClickHouse 笔记1 | 简介、特点 | 基于CentOS7系统的安装与使用 | 常用数据类型 | MergeTree 表引擎 | SQL操作
VLAN trunk experiment
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL