/*

* ConfigurationFile.java

*

* Created on 2009年4月15日, 下午1:36

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* 这是个配置文档操作类,用来读取和配置ini配置文档

* @author 由月

* @version 2004-08-18

* @修改 2008-05-22

*/

public final class ConfigurationFile {

/**

* 从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("//["+section+"//]");

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("=");

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)

throws IOException {

String fileContent, allLine,strLine, newLine, remarkStr;

String getValue;

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

boolean isInSection = false;

fileContent = "";

try {

while ((allLine = bufferedReader.readLine()) != null) {

allLine = allLine.trim();

System.out.println("allLine == "+allLine);

strLine = allLine;

Pattern p;

Matcher m;

p = Pattern.compile("//["+section+"//]");

m = p.matcher((strLine));

//System.out.println("+++++++ ");

if (m.matches()) {

System.out.println("+++++++ ");

p = Pattern.compile("//["+section+"//]");

m = p.matcher(strLine);

if (m.matches()) {

System.out.println("true ");

isInSection = true;

} else {

isInSection = false;

System.out.println("+++++++ ");

}

}

if (isInSection == true) {

strLine = strLine.trim();

String[] strArray = strLine.split("=");

getValue = strArray[0].trim();

if (getValue.equalsIgnoreCase(variable)) {

// newLine = getValue + " = " + value + " " + remarkStr;

newLine = getValue + " = " + value + " ";

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 ex){

throw ex;

} finally {

bufferedReader.close();

}

return false;

}

public static void main(String[] args) {

//String value = Config.getProfileString("sysconfig.ini", "Option", "OracleDB", "default");

//System.out.println(value);

try {

System.out.println("值已经改变!... "+ConfigurationFile.setProfileString("E:/web5/h2_test/gamewww.ini", "TestSect1", "10012", "111111"));

System.out.println("值读取成功!... "+ConfigurationFile.getProfileString("E:/web5/h2_test/gamewww.ini", "TestSect1", "10012", ""));

} catch (IOException e) {

System.out.println("错误 ......"+e.toString());

}

}

}

java 修改ini文件_java读取和修改ini配置文件 | 学步园相关推荐

  1. java 修改ini文件_Java读取和修改ini配置文件

    /** * 修改ini配置文档中变量的值 * @param file 配置文档的路径 * @param section 要修改的变量所在段名称 * @param variable 要修改的变量名称 * ...

  2. java解析yml文件_java读取解析application.yml

    java读取解析application.yml 不用依赖spring容器,可单独使用. bug已修改... 第一步.首先要2个jar com.fasterxml.jackson.dataformat ...

  3. java读写orc文件_java读取hive的orc文件

    hive API2 hive udf 读取HDFS的orc文件 package hive_udf_province; import org.apache.hadoop.conf.Configurati ...

  4. java访问本地文件_java 读取本地文件 更改

    /*** @Title: MakeHtml * @Description: 创建html *@paramfilePath 设定模板文件 *@paramconent 替换的内容 *@paramdisrP ...

  5. java打开db文件_java 读取DB.properties文件方式 | 学步园

    读properties文件的时候,出现了或多或少的奇怪问题,这里把最终成功的方式描述一下. PropUtil工具类 public class PropUtil { private String fil ...

  6. java注解获取参数_JAVA 自定义注解及参数获取 | 学步园

    package com.java.annotation; import java.lang.annotation.*; /** * Created by lw on 14-5-30. * 自定义注解 ...

  7. java socket超时时间_java中socket 默认超时时间 | 学步园

    MS:永远吧 Java代码 connect publicvoidconnect(SocketAddress endpoint, inttimeout) throwsIOException Connec ...

  8. java 中的点_java————形参中的点点点 | 学步园

    转自:http://zhidao.baidu.com/question/149668626.html main方法的签名其实可以这样写: public static void main(String. ...

  9. java libraries在哪_java.library.path在哪? | 学步园

    编译运行可得 Code: publicclassHelloWorld{ publicstaticvoidmain(String args[]){ System.out.println("He ...

最新文章

  1. C#之基本知识和语法3 - 数据类型
  2. python时间计算_python datetime库使用和时间加减计算
  3. 数据结构中基本查找算法总结
  4. 【转载】SQL 模糊查询
  5. SaltStack实战之远程执行-Modules
  6. 计算机python语言_计算机语言Python解释器
  7. 【leetcode】First Missing Positive(hard) ☆
  8. 桂电管理科学可以转计算机,去年调到桂电计算机的感言,希望对各研友有帮助...
  9. 那些年,我们一起找过的工作
  10. 8uftp怎么使用,小编教你8uftp怎么使用
  11. lintcode 168. 吹气球 动态规划
  12. 分析与提取QQ木马盗号技术
  13. 院士专家热议如何拥抱“东数西算”,第二届中国IDC行业Discovery大会顺利召开
  14. Internet Explorer 无法打开 Internet 站点
  15. 周大福心动珠宝,请接收爱的讯号
  16. 我的世界服务器群系修改,我的世界创世神教程 第五十五节修改选区的生物群系|功能介绍|难点介绍|这节...
  17. Gradle学习之Android-DSL AppExtension篇
  18. 基于Win10单机部署kubernetes应用
  19. 如何在windows系统下安装swoole(cgywin环境)
  20. TM7707 评估前做的功课

热门文章

  1. 3. Java 并发
  2. 如何删除计算机中的管理员用户名和密码,电脑系统教程:Win10系统如何删除管理员账户密码...
  3. 手机游戏将迎来黄金期 玩家担心收费不规范
  4. Navicat Premium for Mac 破解教程
  5. 软件发展教育先行 数码大方支持大学生成图大赛
  6. shell 脚本循环语句
  7. 武林风云之服务器删除日志脚本
  8. 08-Django-基础篇-admin管理后台
  9. 抖音小店营业执照怎么办理?无货源怎么选择类目?可收藏
  10. 2020-09-13