偶然在网上找到java可以读写xxx.ini文件
1、首先你需要了解xxx.ini配置文件的组成,如下:
格式
节
参数(键=值)
2、其次你得去网上下载inieditor-r2.jar或者inieditor.jar库
下面以java程序读写取MySQL中配置文件my.ini文件(实现把my.ini文件拷贝到了我建立工程的目录下了)为例子:
注意my.ini文件中包含如下信息:
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
bind-address = 127.0.0.1
port = 3306
我们的目的是修改[mysqld]下的bind-address的值:具体修改如下
/*
* @class MyINIContext.java
* @author ccfeng
* @date 2013-4-2
*
*
*/
package com;
import java.io.IOException;
import ch.ubique.inieditor.IniEditor;
public class MyINIContext {
public final static String DEFAULT_SETTING = "my.ini";
private static IniEditor iniEditor = new IniEditor();
static {
try {
iniEditor.load(DEFAULT_SETTING);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String section,String key){
return iniEditor.get(section, key);
}
public static void saveOrUpdate(String section,String key,String value){
iniEditor.set(section, key, value);
try {
iniEditor.save(DEFAULT_SETTING);
} catch (IOException e) {
e.printStackTrace();
}
}
//TEST
public static void main(String[] args) {
String ip = getValue("mysqld", "bind-address");
System.out.println(ip);
saveOrUpdate("mysqld", "bind-address","192.168.0.103");
ip =getValue("mysqld", "bind-address");
System.out.println(ip);
}
}
输出结果:
127.0.0.1
192.168.0.103