??? 在java中,配置文件一般主要是两种形式:xml文件或者property文件。但大部分人都习惯使用ini文件,而且ini文件的分节以及注释功能,比起xml,也是易懂易用的。
??? 在vc中类库中有读写ini文件的标准函数。在dephi或其他语言中,也可以用windows的api函数来读写ini文件。但在java中似乎没有现成的类和方法可供使用。虽然java可以通过加载dll文件的方法来调用windows的api,但总觉得不够正宗。
??? 于是自己写了个读写ini配置文件的类,供大家参考。
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.util.regex.matcher;
import java.util.regex.pattern;
/**
?* 这是个配置文件操作类,用来读取和设置ini配置文件
?* @author 由月
?* @version 2004-08-18
?*/
public final class configurationfile {
?/**
? * 从ini配置文件中读取变量的值
? * @param file 配置文件的路径
? * @param section 要获取的变量所在段名称
? * @param variable 要获取的变量名称
? * @param defaultvalue 变量名称不存在时的默认值
? * @return 变量的值
? * @throws ioexception 抛出文件操作可能出现的io异常
? */
?public static string getprofilestring(
??string file,
??string section,
??string variable,
??string defaultvalue)
??throws ioexception {
??string strline, value = "";
??bufferedreader bufferedreader = new bufferedreader(new filereader(file));
??boolean isinsection = false;
??try {
???while ((strline = bufferedreader.readline()) != null) {
????strline = strline.trim();
????strline = strline.split("[;]")[0];
????pattern p;
????matcher m;
????p = pattern.compile("\\[\\s*.*\\s*\\]");
????m = p.matcher((strline));
????if (m.matches()) {
?????p = pattern.compile("\\[\\s*" + section + "\\s*\\]");
?????m = p.matcher(strline);
?????if (m.matches()) {
??????isinsection = true;
?????} else {
??????isinsection = false;
?????}
????}
????if (isinsection == true) {
?????strline = strline.trim();
?????string[] strarray = strline.split("=");
?????if (strarray.length == 1) {
??????value = strarray[0].trim();
??????if (value.equalsignorecase(variable)) {
???????value = "";
???????return value;
??????}
?????} else if (strarray.length == 2) {
??????value = strarray[0].trim();
??????if (value.equalsignorecase(variable)) {
???????value = strarray[1].trim();
???????return value;
??????}
?????} else if (strarray.length > 2) {
??????value = strarray[0].trim();
??????if (value.equalsignorecase(variable)) {
???????value = strline.substring(strline.indexof("=") + 1).trim();
???????return value;
??????}
?????}
????}
???}
??} finally {
???bufferedreader.close();
??}
??return defaultvalue;
?}
?/**
?* 修改ini配置文件中变量的值
?* @param file 配置文件的路径
?* @param section 要修改的变量所在段名称
?* @param variable 要修改的变量名称
?* @param value 变量的新值
?* @throws ioexception 抛出文件操作可能出现的io异常
?*/
?public static boolean setprofilestring(
??string file,
??string section,
??string variable,
??string value)
??throws ioexception {
??string filecontent, allline,strline, newline, remarkstr;
??string getvalue;
??bufferedreader bufferedreader = new bufferedreader(new filereader(file));
??boolean isinsection = false;
??filecontent = "";
??try {
???while ((allline = bufferedreader.readline()) != null) {
????allline = allline.trim();
????if (allline.split("[;]").length > 1)
?????remarkstr = ";" + allline.split(";")[1];
????else
?????remarkstr = "";
????strline = allline.split(";")[0];
????pattern p;
????matcher m;
????p = pattern.compile("\\[\\s*.*\\s*\\]");
????m = p.matcher((strline));
????if (m.matches()) {
?????p = pattern.compile("\\[\\s*" + section + "\\s*\\]");
?????m = p.matcher(strline);
?????if (m.matches()) {
??????isinsection = true;
?????} else {
??????isinsection = false;
?????}
????}
????if (isinsection == true) {
?????strline = strline.trim();
?????string[] strarray = strline.split("=");
?????getvalue = strarray[0].trim();
?????if (getvalue.equalsignorecase(variable)) {
??????newline = getvalue + " = " + value + " " + remarkstr;
??????filecontent += newline + "\r\n";
??????while ((allline = bufferedreader.readline()) != null) {
???????filecontent += allline + "\r\n";
??????}
??????bufferedreader.close();
??????bufferedwriter bufferedwriter =
???????new bufferedwriter(new filewriter(file, false));
??????bufferedwriter.write(filecontent);
??????bufferedwriter.flush();
??????bufferedwriter.close();
??????return true;
?????}
????}
????filecontent += allline + "\r\n";
???}
??}catch(ioexception ex){
???throw ex;
??} finally {
???bufferedreader.close();
??}
??return false;
?}
?/**
? * 程序测试
? */
?public static void main(string[] args) {
??//string value = config.getprofilestring("sysconfig.ini", "option", "oracledb", "default");
??//system.out.println(value);
??try {
???system.out.println(configurationfile.setprofilestring("d:/1.ini", "settings", "sampsize", "111"));
??} catch (ioexception e) {
???system.out.println(e.tostring());
??}
??
?}
}
这个类可以读和写ini文件,不过先说明一点:它识别ini文件中的“;”为注释符,而不是识别“#”为注释符。