运行ini文件java,如何在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文件并添加依赖项:

org.ini4j

ini4j

0.5.4

有关此软件包的更多信息, 请访问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

*/

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

*/

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

*/

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值