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. UI第二组与数据库对接时遇到的问题记录

    此为组内某一位做UI的同学的随笔. 之前的app由于没有加入数据库,所以每次重新启动里面的东西都会回到初始状态,即不能保存内容.我们的数据库小组已经很棒地基本完成了数据库的工作,于是我就准备加入数据库 ...

  2. ChatOps如何变革企业业务

    [编者按]本文作者为日志分析软件公司 Logz.io 的联合创始人 Tomer Levy,主要介绍 ChatOps 的特点与发展历程,以及将来可能带来的业务变革.文章系国内 ITOM 管理平台 One ...

  3. LeetCode题解之Binary Number with Alternating Bits

    1.题目描述 2.问题分析 将数值转换为二进制,然后将前面的 0 去掉,再遍历一边二进制字符串,对每个字符和其后部的字符进行比较. 3.代码 bool hasAlternatingBits(int n ...

  4. windows10如何打开vhd文件

    本人电脑安装了Visual Studio 2017,但是由于项目需求需要Core SDK(2.0)的版本支持,也就是2017最新版.所以现在需要利用visual Studio 2017最新版本的安装包 ...

  5. JS里设定延时

    使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function",tim ...

  6. Tmux 使用教程 on Fedora 28

    Tmux 和 gun/screen 都是优秀的终端复用软件,它的关键的好处就是,保持ssh 登录连接session不中断,并且能支持分屏操作,具备丰富的命令行参数,可以进行动态布局和操作.可以看作为s ...

  7. ln -s 软连接介绍

    软连接(softlink)也称符号链接.linux里的软连接文件就类似于windows系统中的快捷方式.软连接文件实际上是一个特殊的文件,文件类型是I.软连接文件实际上可以理解为一个文本文件,这个文件 ...

  8. django模板系统(上)

    filters 过滤 default 替代作用 filesizeformat 格式化为人类可读 add 给变量加参数 lower 小写 upper 大写 title 标题 ljust 左对齐 rjus ...

  9. SQL Server:INFORMATION_SCHEMA.columns 与sys.columns 与 syscolumns对比

    sys.columns视图 sys.columns是SQL Server从2005版本起引入的新的系统级视图.相关链接如下: Mapping SQL Server 2000 System Tables ...

  10. NOIP模拟赛-2018.11.5

    NOIP模拟赛 好像最近每天都会有模拟赛了.今天从高二逃考试跑到高一机房,然而高一也要考试,这回好像没有拒绝的理由了. 今天的模拟赛好像很有技术含量的感觉. T1:xgy断句. 好诡异的题目,首先给出 ...