Preface:
I decided to write an INI read/write class by myself, hoping to help you.
If a friend finds problems, please be sure to send a bicheng.gui@gmail.com
1. What is an INI file?
The NI file is a text file, and the data format in the middle is generally:
[Section1 name]
Keyname1 = value1
Keyname2 = value2
...
[Section2 name]
Keyname1 = value1
Keyname2 = value2
The INI file can be divided into multiple sections. The names of each section are enclosed in. There can be many keys in a section. Each key can have a value and occupy a row. The format is key = value. each line starts with '#' and.
The following is an instance file:
[Database]
DBMS = o84 Oracle
Servername = 192.168.1.1
Database = DB
Databasepassword = 123456
Logpassword = 123456
[Xdatabase]
DBMS = Oracle
Servername = mssql-srv-02
2. Design Ideas
An INI file usually reads a key and updates the value of the key. It is rare to delete or add a key. These two operations are generally not required.
The read/write implementation is like this. First, read the file content into a memory. Read and Write Data in the memory. For the sake of portability,CodeThe standards will be strictly observed. Because the data structures required for reading and writing files are different, two classes are designed, one for reading and the other for writing.
Implementation of the read class: implemented using a nested Map <string, Map <string, string> data structure.
Write class implementation: map and set are sorted by default, which will disrupt the order of sections and keys, so it is not suitable for writing files. For efficiency, you can directly operate on files.