Design and Implementation of an INI file read/write class

Source: Internet
Author: User

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.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.