用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的值不要太长,理论上最多不 ...
随机推荐
- 什么是测试开发工程师-google的解释
什么是测试开发工程师-google的解释 “ 软件测试开发工程师[SET or Software Engineer in Test],和软件开发工程师一样是开发工程师,主要负责软件的可测试性.他们参与 ...
- Math.pow用法及实现探究
pow函数在java.lang.Math类中,是求次方的函数,定义为: public static double pow(double a, double b): 即求a的b次方,例如: public ...
- python css概述
1. 概述 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:style ...
- 微信小程序,前端大梦想(二)
微信小程序的视图与渲染 今天我们从四个方面来了解小程序: •组件的基本使用 •数据绑定 •渲染标签 •模板的使用 一.组件的基本使用: 微信小程序为我们的开发提供了丰富的UI组件 ...
- 1020. Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- WebForm捆绑压缩js和css(WebForm Bundling and Minification)
.net framework 4以上,可以使用Microsoft.AspNet.Web.Optimization 新建4.0项目 Nuget搜索optimization,安装第一个包 加入Bundle ...
- contos 7/redhat 7 安装mysql
1.给网卡配置ip.掩码.网关 2.添加dns,编辑文件:/etc/resolve.conf nameserver 202.96.209.133 //上海本地dns nameserve ...
- iOS开发 socket, 全局socket
因为项目的要求是全局的socket, 哪里都有可能使用到socket去发消息, 所以我把socket写在了单利里面 项目用的是 pod 'CocoaAsyncSocket' 三方库, 是异步的, ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- C语言的一些基础
一.C语言基础: 1.1.main函数是入口函数,用于进行link. 1.2..sln是解决方案的管理文件. 1.3.int:32位.short:16位.long:32位.long long:64位. ...