本文转载地址:

      http://www.blogjava.net/silvernapoleon/archive/2006/08/07/62222.html


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class ConfigWriter { /**
* 这是个配置文件操作类,用来读取和设置ini配置文件
* @author 由月
* @version 2004-08-18
*/
/**
* 从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("file://[//s*.*//s*//]");
m = p.matcher((strLine));
if(m.matches()) {
p = Pattern.compile("file://[//s* " + section + " file://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) {
String fileContent, allLine, strLine, newLine, remarkStr;
String getValue;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
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]; strLine = allLine.trim();
Pattern p;
Matcher m;
p = Pattern.compile("file://[//s*.*//s*//]");
m = p.matcher((strLine)); if(m.matches()) {
p = Pattern.compile("file://[//s*" + section + "file://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 ie) {
ie.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} return false;
} /**
* 程序测试
*/
public static void main(String[] args) {
// String value = Config.getProfileString("sysconfig.ini", "Option", "OracleDB", "default");
// System.out.println(value); System.out.println(ConfigWriter.setProfileString("d:/1.ini", "Settings", "SampSize", "111"));
}
}

用java读写ini配置文件的更多相关文章

  1. java 读写ini配置文件

    ini配置文件 ;客户端配置[Client];客户端版本号version=0001;设备号devNum=6405 public final class ConfigurationFile { /** ...

  2. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  3. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

  4. C# 读写 ini 配置文件

    虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...

  5. [转]VB 读写ini 配置文件

    转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...

  6. 自己写的 读写 ini 配置文件类

    /// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...

  7. 引用“kernel32”读写ini配置文件

    引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件  引用"kernel32"读写ini配置文件 OverView ke ...

  8. C# 文件的一些基本操作(转)//用C#读写ini配置文件

    C# 文件的一些基本操作 2009-07-19  来自:博客园  字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...

  9. C#操作读写INI配置文件

    一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...

随机推荐

  1. 常用PHP函数整理

    is_upload_file() 判断文件是不是通过HTTP POST 方式上传来的in_array() 判断变量在不在数组范围内move_uploaded_file() 将上传的临时名移到指定文件夹 ...

  2. 六行python代码的爱心曲线

    前些日子在做绩效体系的时候,遇到了一件囧事,居然忘记怎样在Excel上拟合正态分布了,尽管在第二天重新拾起了Excel中那几个常见的函数和图像的做法,还是十分的惭愧.实际上,当时有效偏颇了,忽略了问题 ...

  3. Timer,TimerTask通过程序计数器实现的定时任务

    1.程序计数器 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用可以看 做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型, 各种虚 ...

  4. CSS3-渐变背景色

    线性渐变背景色: <style> .linear { width:130px; height:130px; border:2px solid black; padding: 10px; b ...

  5. CGLIB和JDK代理

    需要的架包:在spring中提供对CGLIB的支持 一.JDK的动态代理 1.接口IUserDao package cn.itcast.spring3.jdk.proxy; public interf ...

  6. 【2017-04-18】Ado.Net C#连接数据库进行增、删、改、查

    一.简介 1.ado.net是一门数据库访问技术. 他可以通过程序来操作数据库 2.类库 Connection 类 和数据库交互,必须连接它.连接帮助指明数据库服务器.数据库名字.用户名.密码,和连接 ...

  7. php写流程管理

    流程控制即某个人发起一个流程,通过一层一层审核,通过后,完成整个流程,若有一层审核未通过,中断整个流程.即结束! 比如请假流程: 某一员工发起一个请假流程,那么这个流程的节点人员即他的上级,上上级,上 ...

  8. C#处理JSON 数据

    网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...

  9. div模拟输入框input/textarea

    //html<!--填写信息--> <div class="info-wrap"> <form class="formToCheck&quo ...

  10. 现有‘abcdefghijkl’12个字符,将其所有的排列按字典序进行排序,给出任意一组排列,说出这租排列在所有排列中是第几小的

    题目: 现有‘abcdefghijkl’12个字符,将其所有的排列按字典序进行排序,给出任意一组排列,说出这租排列在所有排列中是第几小的 据说这道题是百度校招的一道算法题,反正我觉得我在学校的时候很可 ...