用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的值不要太长,理论上最多不 ...
随机推荐
- es6编写reactjs事件处理函数绑定this三种方式
第一种:官方推荐的: class LoginControl extends React.Component { constructor(props) { super(props); this.hand ...
- 什么是测试开发工程师-google的解释
什么是测试开发工程师-google的解释 “ 软件测试开发工程师[SET or Software Engineer in Test],和软件开发工程师一样是开发工程师,主要负责软件的可测试性.他们参与 ...
- WebApp框架
我所知道的webapp开发框架,欢迎补充, Framework7包含ios和material两种主题风格并且有vue版和react版, vue发现一个vue-material, react有一款mat ...
- ACdream 1112 Alice and Bob (sg函数的变形+素数筛)
题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数: 或者可以减少为a或b,Alice先,问谁能赢 思路 ...
- ios坐标位置转换
//ios常用坐标转换来处理一些下拉框队形的按钮的位置,我以最下面两个来进行一下个人的理解,不足之处多多见谅 - (CGPoint)convertPoint:(CGPoint)point toView ...
- 1147: 零起点学算法54——Fibonacc
1147: 零起点学算法54--Fibonacc Time Limit: 1 Sec Memory Limit: 64 MB 64bit IO Format: %lldSubmitted: 20 ...
- less学习笔记(一)
less的写法如下 .content { ul{ list-style: none; } li{ height: 25px; line-height: 25px; padding-left: 15px ...
- lua 字符串
lua 字符串 语法 单引号 双引号 "[[字符串]]" 示例程序 local name1 = 'liao1' local name2 = "liao2" lo ...
- vscode同步设置&扩展插件
首先安装同步插件: Settings Sync 第二部进入你的github如图: 打开设置选项: 新建一个token: 如图: 记住这个token值 转到vscode 按shift+alt +u ...
- 统计学习方法:核函数(Kernel function)
作者:桂. 时间:2017-04-26 12:17:42 链接:http://www.cnblogs.com/xingshansi/p/6767980.html 前言 之前分析的感知机.主成分分析( ...