读取 ini 文件,Ini4j框架的使用

来源:互联网 发布:mysql主键和外键 编辑:程序博客网 时间:2024/05/16 05:54

Ini4j 是一款操作文本行的配置文件的工具,网站对如何使用作了介绍 http://ini4j.sourceforge.net/ 。

他实现了可以读取类似 *.ini 文件格式的配置文件。

ini4j的maven pom.xml

Xml代码  收藏代码
  1. <dependency>  
  2.   <groupId>org.ini4j</groupId>  
  3.   <artifactId>ini4j</artifactId>  
  4.   <version>0.5.2</version>  
  5. </dependency>  

 

配置文件如下:

hello.conf 写道
#############################################
# ini4j example #
#############################################

[system]
program_name=ini4jExample
version=1.0

[person_1]
name=kanpiaoxue_1
age=30
sex=1

[person_2]
name=kanpiaoxue_2
age=31
sex=1

[company]
name=company1
address=beijing

[company]
name=company1
address=beijing

 java代码如下:

Java代码  收藏代码
  1. import org.apache.commons.lang3.StringUtils;  
  2. import org.ini4j.Config;  
  3. import org.ini4j.Ini;  
  4. import org.ini4j.Profile.Section;  
  5.   
  6. import com.google.common.io.Resources;  
  7.   
  8. import java.io.IOException;  
  9. import java.net.URL;  
  10. import java.util.Map.Entry;  
  11. import java.util.Set;  
  12.   
  13. /** 
  14.  * <pre> 
  15.  * Init4jExample.java 
  16.  * @author kanpiaoxue<br> 
  17.  * @version 1.0 
  18.  * Create Time 2014年7月5日 下午12:43:04<br> 
  19.  * Description : init4j 的使用 
  20.  * </pre> 
  21.  */  
  22. public class Init4jExample {  
  23.     private static final String CONFIG_NAME = "hello.conf";  
  24.     private static final String SYSTEM = "system";  
  25.     private static final String COMPANY = "company";  
  26.     private static final String PROGRAM_NAME = "program_name";  
  27.     private static final String VERSION = "version";  
  28.     private static final String NAME = "name";  
  29.     private static final String AGE = "age";  
  30.     private static final String SEX = "sex";  
  31.     private static final String ADDRESS = "address";  
  32.   
  33.     /** 
  34.      * <pre> 
  35.      * @param args 
  36.      * </pre> 
  37.      */  
  38.     public static void main(String[] args) {  
  39.   
  40.         Config cfg = new Config();  
  41.         // 生成配置文件的URL  
  42.         URL url = Resources.getResource(CONFIG_NAME);  
  43.         // 设置Section允许出现重复  
  44.         cfg.setMultiSection(true);  
  45.         Ini ini = new Ini();  
  46.         ini.setConfig(cfg);  
  47.         try {  
  48.             // 加载配置文件  
  49.             ini.load(url);  
  50.             System.out.println(StringUtils.center(SYSTEM, 50'='));  
  51.             // 读取 system  
  52.             Section section = ini.get(SYSTEM);  
  53.             System.out.println(PROGRAM_NAME + " : " + section.get(PROGRAM_NAME));  
  54.             System.out.println(VERSION + " : " + section.get(VERSION));  
  55.   
  56.             // 读取没有规律的person系列  
  57.             System.out.println(StringUtils.center("person"50'='));  
  58.             Set<Entry<String, Section>> set = ini.entrySet();  
  59.             for (Entry<String, Section> entry : set) {  
  60.                 String sectionName = entry.getKey();  
  61.                 // 跳过 system 和 company  
  62.                 if (!SYSTEM.equals(sectionName) && !COMPANY.equals(sectionName)) {  
  63.                     System.out.println(NAME + " : " + entry.getValue().get(NAME));  
  64.                     System.out.println(AGE + " : " + entry.getValue().get(AGE));  
  65.                     System.out.println(SEX + " : " + entry.getValue().get(SEX));  
  66.                 }  
  67.             }  
  68.   
  69.             // 读取具有相同 Section 的 company  
  70.             System.out.println(StringUtils.center(COMPANY, 50'='));  
  71.             for (Section session : ini.getAll(COMPANY)) {  
  72.                 System.out.println(NAME + " : " + session.get(NAME));  
  73.                 System.out.println(ADDRESS + " : " + session.get(ADDRESS));  
  74.             }  
  75.         } catch (IOException e) {  
  76.             e.printStackTrace();  
  77.         }  
  78.     }  
  79.   
  80. }  
0 0
原创粉丝点击