ini file Operations Guide

  Today article summarizes a toolbox.

  ini file types commonly used as initialization file for the program. Different from our common configuration files throughout key-value in the form of key-value pairs, ini file on the basis of the above as well as classification key nodes, such as initial configuration file my.cnf our common Mysql database or my.ini , the contents of this format is generally as follows:

[client]
port = 3306
socket = /data/3306/mysql.sock

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data

  For a read operation of a file in this format, Java classes are commonly used Properties of not working. Of course, you can own the line and code level with superb handwriting tool method to read and write, but certainly it is quite nerve-racking. In fact, we have a third-party utility libraries to choose from. Here bloggers shared library called org.dtools.javaini . The whole kit is lightweight, can support basic literacy, format verification, etc., the official tutorial is very detailed, spend very little time to get started. Of course, in conjunction with the project, it is still required to further their own package of these methods in order to better use. Predecessors trees, descendants cool, bloggers throwing quoted Yu also wrote a few:

Package in module.ini; 

Import com.alibaba.fastjson.JSONObject;
 Import org.dtools.ini *. ;
 Import java.io.File;
 Import java.io.IOException;
 Import the java.util.Iterator;
 Import a java.util.Map ;
 Import java.util.Set; 

/ ** 
 * @date: 2019/10/16 19:11 
 * @author : Chen 
 * @desc: org.dtools.javaini-v1.1.00.jar ini file reader tool kit class 
 * / 
public  class IniUtil { 

    // INI file abstracted 
    Private  static the iniFile iniFile = null ;
     //Examples of documents to operate 
    Private  static File File = null ; 

    // operations ini file reader and writer to read and write operations performed by their specific 
    Private IniFileReader iniFileReader = null ;
     Private IniFileWriter iniFileWriter = null ; 

    / ** 
     * @param filePath file path 
     * @param caseSensitive case sensitivity defaults to false 
     * @param Validator checker format
      * / 
    public IniUtil (String filePath, IniValidator Validator, Boolean caseSensitive) { 
        file = new new  file (filePath);
        iniFile= new BasicIniFile(validator, caseSensitive);
        try {
            init();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public IniUtil(String filePath) {
        this(filePath, new IniValidator(), false);
    }

    public IniUtil(String filePath, IniValidator validator) {
        this(filePath, validator, false);
    }

    public IniUtil(String filePath, booleancaseSensitive) {
        this (filePath, new new IniValidator (), caseSensitive); 
    } 

    / ** 
     * ini initialization files reader and writer 
     * / 
    Private  void the init () throws IOException { 
        iniFileReader = new new IniFileReader (iniFile, File); 
        iniFileWriter = new new IniFileWriter (iniFile, file); 
        iniFileReader.read (); 
    } 

    / ** 
     * ini file read data is converted Json objects 
     * / 
    public the JSONObject getIniFileInfo () { 
        the Iterator <IniSection> sectionIterator = iniFile.iterator (); 
        the jSONObject jsonObject = new new JSONObject();
        while (sectionIterator.hasNext()) {
            IniSection section = sectionIterator.next();
            Iterator<IniItem> itemIterator = section.iterator();
            JSONObject child = new JSONObject();
            while (itemIterator.hasNext()) {
                IniItem item = itemIterator.next();
                child.put(item.getName(), item.getValue());
            }
            jsonObject.put(section.getName(), child);
        }
        return jsonObject;
    }

    /**
     * Get all the keys of a node 
     * / 
    public the JSONObject getIniBySection (String sectionName) throws Exception { 
        IniSection sectionTop = GetSection (sectionName); 
        the Iterator <IniItem> Iterator = section.iterator (); 
        the JSONObject jsonObject = new new the JSONObject () ;
         the while (iterator.hasNext ()) { 
            IniItem Item = Iterator.next (); 
            jsonObject.put (item.getName (), item.getValue ()); 
        } 
        return jsonObject; 
    } 


    / ** public
     * Get the value of an entry in a node 
     * /
     Object getItemByName (sectionName String, String itemName) throws Exception { 
        the JSONObject iniBySection = the this .getIniBySection (sectionName);
         return iniBySection.get (itemName); 
    } 


    / ** 
     * add or modify a node entry 
     * / 
    public  void addOrUpdateItem (String sectionName, itemName String, String itemValue) throws Exception { 
        IniSection sectionTop = GetSection (sectionName); 
        iniItem iniItem = section.addItem (itemName);
         IF (iniItem == null)
            iniItem = Section.getItem (itemName); 
        iniItem.setValue (itemValue); 
        iniFileWriter.write (); 
    } 


    / ** 
     * add or modify a node entries may add notes 
     * / 
    public  void addOrUpdateItem (String sectionName, itemName String, String itemValue , Comment String) throws Exception { 
        IniSection sectionTop = GetSection (sectionName); 
        iniItem iniItem = section.addItem (itemName);
         IF (iniItem == null ) 
            iniItem = section.getItem (itemName);
        iniItem.setValue(itemValue);
        iniItem.setPreComment (Comment);   // prepend Note 
        iniFileWriter.write (); 
    } 


    / ** 
     * add or modify a node entry 
     * / 
    public  void addOrUpdateItems (String sectionName, the Map <String, Object> itemMap) throws Exception { 
        IniSection sectionTop = iniFile.getSection (sectionName); 
        the Set <String> items = itemMap.keySet (); 
        section.addItems (items.toArray ( new new String [0 ])); 
        iniFileWriter.write (); 
    } 

    / ** 
     * Add node 
     * / 
    public  void addSection(String sectionName) throws IOException {
        if (iniFile.addSection(sectionName) == null)
            return;
        iniFileWriter.write();
    }


    /**
     * 删除 某节点
     */
    public void removeSection(String sectionName) throws IOException {
        if (iniFile.removeSection(sectionName) == null)
            return;
        iniFileWriter.write();
    }

    /**
     * 删除 某条目
     */
    public void removeItem(String sectionName, String itemName) throws Exception {
        IniSection section = getSection(sectionName);
        section.removeItem(itemName);
        iniFileWriter.write();
    }


    private IniSection getSection(String sectionName) throws Exception {
        if (!iniFile.hasSection(sectionName)) {
            throw new Exception("The ini file【" + file.getName() + "】 has no section named " + sectionName);
        }
        return iniFile.getSection(sectionName);
    }

}

  As a relatively simple toolkit to address basic types ini file deletions to change search enough. If you want to use the depth, but also some of the problems you need to solve their own -

    ① toolkit files using the default ASCII encoding, so the characters outside of the ASCII code, such as Chinese characters can not be supported, if the Chinese will be garbled reading and writing process;
    ② support for annotations is not enough. Common comments for #, but only to the author but to; and a number of comments at the beginning // ,, if the original file has # type of comment, the read and write operations will be cleared away;

  Since the class library does not provide a way to set the encoding or comments marked, therefore, a better way is to download their own source code, make changes in the source code, and then packaged into their own projects using. The following is a blogger code changes the experience -

    First, the problem of characters, the author is defined in the class IniFIleWriter represents a constant character, remove the final modifier, ease of use dynamically modify the encoding process:

                

 

   Secondly, the comment, the author defined in Commentable interface represents the beginning of a comment ';' identity, because it is declared in the interface, the default is static final, so we wanted to change dynamically set more difficult task, changes will be more, good practice is the most simple and crude way - directly into the usual # comment symbol on it:

                

 

 

  Finally, re-labeled jar into your project or put in your maven repository. Well, tools, methods, on a long story short, I wish a happy use.

  If you have any problems or mistakes, welcome message exchange, be grateful!

 

Guess you like

Origin www.cnblogs.com/chenbenbuyi/p/11689497.html