用java读写ini配置文件
本文转载地址:
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配置文件的更多相关文章
- java 读写ini配置文件
		
ini配置文件 ;客户端配置[Client];客户端版本号version=0001;设备号devNum=6405 public final class ConfigurationFile { /** ...
 - 【转】Java 读写Properties配置文件
		
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
 - Java 读写Properties配置文件
		
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
 - C# 读写 ini 配置文件
		
虽说 XML 文件越发流行,但精简的 ini 配置文件还是经常会用到,在此留个脚印. 当然,文中只是调用系统API,不会报错,如有必要,也可以直接以流形式读取 ini文件并解析. /// <su ...
 - [转]VB 读写ini 配置文件
		
转自 百度知道 C# 读写 ini配置文件 点此链接 'API 声明Public Declare Function GetPrivateProfileString Lib "kernel32 ...
 - 自己写的 读写 ini 配置文件类
		
/// <summary> /// 不调用系统API 读写 ini 配置文件 /// </summary> public class RW_ini { #region ==== ...
 - 引用“kernel32”读写ini配置文件
		
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
 - C# 文件的一些基本操作(转)//用C#读写ini配置文件
		
C# 文件的一些基本操作 2009-07-19 来自:博客园 字体大小:[大 中 小] 摘要:介绍C#对文件的一些基本操作,读写等. using System;using System.IO;us ...
 - C#操作读写INI配置文件
		
一个完整的INI文件格式由节(section).键(key).值(value)组成.示例如:[section]key1=value1key2=value2; 备注:value的值不要太长,理论上最多不 ...
 
随机推荐
- 整合初步______SH
			
什么是框架 在的J2EE开发中,经常会提到"框架"这个词汇,例如Spring,Struts,Webx等等都称之为J2EE开发框架.那么,什么是框架呢? 框架的英文为Framewor ...
 - POPTEST老李分享session,cookie的安全性以及区别 2
			
四,session和cookie谁更安全 就个人而言,我觉得session更安全一点,我以下几点看法. 1,如果session和cookie一样安全的话,二者就没有并要同时存在了,只要cookie就好 ...
 - 手机自动化测试:搭建appium手机自动化测试开发环境
			
手机自动化测试:搭建appium手机自动化测试开发环境 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大 ...
 - [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据
			
原文地址:http://www.dotnetcurry.com/aspnet/1354/elastic-search-kibana-in-docker-dotnet-core-app 想要轻松地通过许 ...
 - 在Caffe中实现模型融合
			
模型融合 有的时候我们手头可能有了若干个已经训练好的模型,这些模型可能是同样的结构,也可能是不同的结构,训练模型的数据可能是同一批,也可能不同.无论是出于要通过ensemble提升性能的目的,还是要设 ...
 - java 客户端发起http请求
			
package com.mall.core.utils.http; import org.apache.commons.lang.StringUtils; import org.apache.http ...
 - CSharpGL(41)改进获取字形贴图的方法
			
CSharpGL(41)改进获取字形贴图的方法 在(http://www.cnblogs.com/bitzhuwei/p/CSharpGL-28-simplest-way-to-creating-fo ...
 - Java多线程学习笔记(二)——Executor,Executors,ExecutorService比较
			
Executor:是Java线程池的超级接口:提供一个execute(Runnable command)方法;我们一般用它的继承接口ExecutorService. Executors:是java.u ...
 - 详解Executor框架
			
在Java中,使用线程来异步执行任务.Java线程的创建与销毁需要一定的开销,如果我们为每一个任务创建一个新线程来执行,这些线程的创建与销毁将消耗大量的计算资源.同时,为每一个任务创建一个新线程来执行 ...
 - open vswitch常用操作
			
以下操作都需要root权限运行,在所有命令中br0表示网桥名称,eth0为网卡名称. 添加网桥: #ovs-vsctl add-br br0 列出open vswitch中的所有网桥: #ovs-vs ...