ini配置文件


;客户端配置
[Client]
;客户端版本号
version=0001
;设备号
devNum=6405


public final class ConfigurationFile {
/**
* 从ini配置文件中读取变量的值
*
* @param file 配置文件的路径
* @param section 要获取的变量所在段名称
* @param variable 要获取的变量名称
* @param defaultValue 变量名称不存在时的默认值
* @return 变量的值
* @throws IOException 抛出文件操作可能出现的io异常
*/
public static String readCfgValue(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("\\[\\w+]");//Pattern.compile("file://[//s*.*//s*//]");
m = p.matcher((strLine));
if (m.matches()) {
p = Pattern.compile("\\[" + section + "\\]");//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 writeCfgValue(String file, String section, String variable, String value) throws IOException {
String fileContent, allLine, strLine, newLine, remarkStr = "";
String getValue = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
boolean isInSection = false;
boolean canAdd = true;
fileContent = "";
try { while ((allLine = bufferedReader.readLine()) != null) {
allLine = allLine.trim();
strLine = allLine.split(";")[0];
Pattern p;
Matcher m;
p = Pattern.compile("\\[\\w+]");
m = p.matcher((strLine));
if (m.matches()) {
p = Pattern.compile("\\[" + section + "\\]");
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;
fileContent += newLine;
while ((allLine = bufferedReader.readLine()) != null) {
fileContent += "\r\n" + allLine;
}
bufferedReader.close();
canAdd = false;
System.out.println(fileContent);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close(); return true;
} }
fileContent += allLine + "\r\n";
}
if (canAdd) {
String str = variable + "=" + value;
fileContent += str + "\r\n";
System.out.println(fileContent);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close();
}
} catch (IOException ex) {
throw ex;
} finally {
bufferedReader.close();
}
return false;
}

ini配置文件中的注释使用【;】表示

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

  1. 用java读写ini配置文件

    本文转载地址:       http://www.blogjava.net/silvernapoleon/archive/2006/08/07/62222.html import java.io.Bu ...

  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. [Android] 压缩图片并保存

    不难,但用的时候有时候突然会想不起来..记录一下吧 原文地址请保留http://www.cnblogs.com/rossoneri/p/3995096.html 先加权限 <uses-permi ...

  2. 8.1、包,__init__.py,

    包: 为了组织好模块,将多个模块组合为一个包,所以包用于存放python模块 包通常是一个文件夹,当文件夹当作包使用时,文件夹需要包含__init__.py文件 __init__.py的内容可以为空, ...

  3. AOP编程报错Xlint:invalidAbsoluteTypeName

    @Component@Aspectpublic class DingdingAspect { private Logger logger = LoggerFactory.getLogger(this. ...

  4. LeetCode题解之Number of Segments in a String

    1.题目描述 2.题目分析 找到字符串中的空格即可 3.代码 int countSegments(string s) { ){ ; } vector<string> v; ; i < ...

  5. 单元测试,模拟用户Get登陆,并携带登录后的token访问接口

    HttpClient _httpClient; HttpClient _businessHttpClient; private async Task<string> GetAccessTo ...

  6. sqlserver性能调优中的逻辑读,物理读,预读是什么意思

    表 'T_EPZ_INOUT_ENTRY_DETAIL'.扫描计数 1,逻辑读 4825 次,物理读 6 次,预读 19672 次.SQL SERVER 数据库引擎当遇到一个查询语句时,SQL SER ...

  7. length() 用法

    length:(字符串 | 列):求出字符串的长度: 求出bqh4表中zym列的长度. select * from bqh4select a.*,length(zym) from bqh4 a

  8. 解决iPhone滑动时滑到另一个层级导致卡顿问题

    问题概览: 两个div都可以滑动时,会造成滑动顶层div时,底层div也会跟着滑动.如图示. 解决方法: 添加CSS即可. 代码如下 * { -webkit-overflow-scrolling: t ...

  9. Excel思考问题的方式

    Excel思考问题的方式 一.写需求,说我要什么数据 好比如,现在咱们需要将第一周.第二周.第三周.第四周.….等E:E列里的"每一周的 第二个数值"提取出来.那么我们手动提取了几 ...

  10. LINE学习

    LINE Abstract LINE 是一种将大规模网络结点表征成低维向量的算法,可很方便用于网络可视化,结点分类,链路预测,推荐. source code Advantage LINE相比于其他算法 ...