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. 如何在 Windows 10 中搭建 Node.js 环境?

    [编者按]本文作者为 Szabolcs Kurdi,主要通过生动的实例介绍如何在 Windows 10 中搭建 Node.js 环境.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 在本文中 ...

  3. 从零自学Java-2.初步理解Java程序使如何工作的

    1.学习Java应用程序是如何工作的 2.构成一个应用程序 3.向应用程序传递参数 4.学习Java程序是如何组织的 5.在应用程序中创建一个对象 程序Root:输出225的正平方根 package ...

  4. sql求两表的并集、交集、非交集、差集、结果集排序

    create table A( id ,) Not null primary key, name ) not null default(''), ) INSERT INTO [A]([name]) V ...

  5. 基元用户模式构造--互锁构造 Interlocked 实现的异步web请求实例

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  6. IHttpModule 和 IHttpHandler 配置方法

    <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettin ...

  7. 关联与下钻:快速定位MySQL性能瓶颈的制胜手段

    本文根据DBAplus社群[2018年1月6日北京开源与架构技术沙龙]现场演讲内容整理而成. 讲师介绍 李季鹏 新炬网络数据库专家 专注于MySQL数据库性能管理及相关解决方案,目前主要从事MySQL ...

  8. DLL导出类避免地狱问题的完美解决方案

    DLL动态链接库是程序复用的重要方式,DLL可以导出函数,使函数被多个程序复用,DLL中的函数实现可以被修改而无需重新编译和连接使用该DLL的应用程序.作为一名面向对象的程序员,希望DLL可以导出类, ...

  9. Windows API串口编程详解

    (一)Windows API串口通信编程概述 Windows环境下的串口编程与DOS环境下的串口编程有很大不同.Windows环境下的编程的最大特征之一就是设备无关性,它通过设备驱动程序将Window ...

  10. FZU Monthly-201901 获奖名单

    FZU Monthly-201901 获奖名单 冠军: S031702338 郑学贵 一等奖: S031702524 罗继鸿 S031702647 黄海东 二等奖: S031702413 韩洪威 S0 ...