java ini jar_Java语言INI处理类和INI文件格式引导

Introduction简介

This article is about a Java class to read and write Windows INI files. One good feature of this class is its support for environment variables, i.e., this class allows us to include a reference to an environment variable while defining values in INI files. The following code snippet depicts this feature:介绍一个类,用于读写Windows INI文件。本类的一大特性是支持环境变量,也就是说,在定义INI文件内的值时,它允许传入环境变量的引用。下面的代码片段解释了此特点:

;Sample INI file,注释

[Database]

UserId = DBuser

Sid = DBSid

DBHost = 145.101.56.32

DBPort = 1521

DBLib = %ORACLE_HOME%\lib\classes12.jar

这里介绍下ini文件,它基于文本文件进行了扩充,用一些特殊符号把不同行组成了节,在节里一行可以表示一个属性。节的开始是带有[]标志的行,在开始行到在下个[]行之间的都是属于本节。带=号的是属性行,=前是属性名,=后是值。

When the DBLib variable is read, the class will try to retrieve the value for the %ORACLE_HOME% variable. Let's say on your system %ORACLE_HOME% points to theC:\Oracle directory, then DBLib will get expanded to C:\Oracle\lib\classes12.jar.当读取DBLib变量时,类会深度获取%ORACLE_HOME%环境变量的值。假定%ORACLE_HOME%值是C:\Oracle,则DBLib会转换为C:\Oracle\lib\classes12.jar

Using the Code代码用法

The following code snippet shows the usage of this class. The same code can also be found in the main method of the INIFile class.

public static void main(String[] pstrArgs)

{

INIFile objINI = null;

String  strFile = null;

if (pstrArgs.length == 0) return;

strFile = pstrArgs[0];

/* Following call loads the strFile if it exists. */

objINI = new INIFile(strFile);

objINI.setStringProperty("Database", "SID", "ORCL", "Database SID");

objINI.setStringProperty("Database", "UserId", "System", "User Id");

objINI.setStringProperty("Database", "Password", "Manager", "Password");

objINI.setStringProperty("Database", "HostName", "DBServer", "Server Host");

objINI.setIntegerProperty("Database", "Port", 1521, "Server Port");

/* Save changes back to strFile */

objINI.save();

objINI = null;

}

Date and Timestamps Usage日期和时间用法

Since all data in INI files is stored as strings, the class provides the following methods to interpret the date and timestamp values in the correct manner.因为ini文件里以字符串存储数据,类提供了下面方法来正确解析日期和时间数据

•       setDateFormat - This method allows to set the date format to be used while converting date strings to date data type and vice versa.此方法允许设置日期格式,该格式在转换日期字符串到日期类型变量时使用,反之亦然

•       setTimeFormat - This method allows to set the timestamp format to be used while converting timestamp strings to timestamp data type and vice versa. 此方法允许设置时间格式,该格式在转换时间字符串到时间类型变量时使用,反之亦然

For the supported date time formats, please refer to java.text.SimpleDateFormat.日期时间格式支持请参考java.text.SimpleDateFormat

Methods Summary方法总结

The class exposes the following public methods to access INI property values. All these methods require that the INI section name and property name be passed as input parameters.这些方法需要ini节名和属性名做为参数。

•       getBooleanProperty - Returns boolean value返回布尔值

•       getStringProperty - Returns string value返回字符串值

•       getIntegerProperty - Returns int value返回整数值

•       getLongProperty - Returns long value返回长整数

•       getDoubleProperty - Returns double value返回双精度

•       getDateProperty - Returns java.util.Date value返回日期

•       getTimestampProperty - Returns java.sql.Timestamp value返回时间

Additionally, the class also provides the following additional methods to retrieve the names of all the sections present in an INI file, names of all the properties present under a particular section, remove the property, remove a section, and save changes back to the disk. The load method is automatically called from the constructor.另外,类还提供下面的方法来获取ini文件里的所有节名,某个节下所有属性名,删除属性,删除,修改结果回写到磁盘上。构造函数里自动调用加载函数。

•       getAllSectionNames - Returns a string array of section names返回节名数组

•       getAllPropertyNames - Returns a string array of section names. This method requires a section name as an input parameter.返回属性名字符串数组。需要节名做为参数

•       removeProperty - Removes specified property from the specified section从指定节里删除某个属性

•       removeSection - Removes the specified section删除节

•       save - Persist changes back to INI file持久化修改到ini文件里。

I hope this will be useful to anyone who needs this functionality in Java.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值