个性化阅读
专注于IT技术分析

如何在Java中轻松地从INI文件读取(解析)和写入INI文件

本文概述

INI格式的配置文件使用户可以通过文件轻松地动态修改应用程序的各个方面。在本文中, 我们将向你展示如何使用Ini4j库在Java中轻松地解析和写入INI文件。

1.包含Ini4j库

Java默认情况下不提供任何集成功能来解析或创建INI文件, 而是你将需要依赖第三方库/程序包。在本教程中, 我们将使用Ini4j库。 ini4j是用于处理Windows .ini格式的配置文件的简单Java API。此外, 该库还包括基于.ini文件的Java Preferences API实现。你可以从maven存储库下载软件包的jar文件, 然后将其手动包含在你的项目中, 或者如果你的项目基于maven, 则可以编辑pom.xml文件并添加依赖项:

<!-- https://mvnrepository.com/artifact/org.ini4j/ini4j -->
<dependency>
    <groupId>org.ini4j</groupId>
    <artifactId>ini4j</artifactId>
    <version>0.5.4</version>
</dependency>

有关此软件包的更多信息, 请访问Maven的官方存储库或官方网站。包含库之后, 你将可以使用import org.ini4j。*导入软件包。在你的代码中。

2.从ini文件中读取数据

假设我们有一个ini文件(myinifile.ini), 其内容如下:

; Last modified 1 January 2019 by John Doe
; Example of INI File for Java
; myinifile.ini
[owner]
name=John Doe
organization=Acme Widgets Inc.
age=24
height=176

[database]
server=192.0.2.62     
port=143
file="payroll.dat"

使用Ini4j接口很容易获得这些值。基本上, 你需要实例化一个Wini类, 该类希望将File类作为你要解析的ini文件的路径的第一个参数, 返回的对象将允许你与该库的API进行交互, 从而非常容易从中获取值。你需要从对象使用的方法是get, 该方法最多需要3个参数:

  • 你要从中获取值的部分的名称。
  • 所选部分中的属性名称。
  • 将检索的值的类型(仅适用于原始值, 如果未设置, 则将返回String)。

例如:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            int age = ini.get("owner", "age", int.class);
            double height = ini.get("owner", "height", double.class);
            String server = ini.get("database", "server");
            
            
            System.out.print("Age: " + age + "\n");
            System.out.print("Geight: " + height + "\n");
            System.out.print("Server IP: " + server + "\n");
        // To catch basically any error related to finding the file e.g
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

3.将数据写入ini文件

写入文件意味着插入/删除/更新任务:

定义属性值

若要更改节中的属性的值, 请使用Wini类实例中放置的方法。此方法最多需要3个参数:

  • 你要从中更新属性的部分
  • 你要更改的属性的名称
  • 物业的新价值

最后, 使用store方法将更改保存到文件中:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            
            ini.put("block_name", "property_name", "value");
            ini.put("block_name", "property_name_2", 45.6);
            ini.store();
        // To catch basically any error related to writing to the file
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

删除整个部分

如果你愿意从文件中删除整个节, 请使用需要你要删除的节名称的remove方法, 然后使用store方法保存更改:

package com.ourcodeworld.mavensandbox;

// Import required classes
import java.io.File;
import org.ini4j.*;

/**
 * Basic example of the use of the ini4j library.
 * 
 * @author Carlos Delgado <dev@ourcodeworld.com>
 */
public class Index {
    
    public static void main(String[] args){
        try{
            Wini ini = new Wini(new File("C:\\Users\\sdkca\\Desktop\\myinifile.ini"));
            
            ini.remove("section_name");
            ini.store();
        // To catch basically any error related to writing to the file
        // (The system cannot find the file specified)
        }catch(Exception e){
            System.err.println(e.getMessage());
        }
    }
}

编码愉快!

赞(0)
未经允许不得转载:srcmini » 如何在Java中轻松地从INI文件读取(解析)和写入INI文件

评论 抢沙发

评论前必须登录!