ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java ini文件处理

2019-08-19 18:00:23  阅读:252  来源: 互联网

标签:trim 文件 java String strArray value ini strLine


java读写ini文件有多种方法,这里将介绍3种方法。

1. 使用工具类Java INI package

该工具的下载和介绍地址是:http://javaini.sourceforge.net/

2.使用工具类ini4j

ini4j的功能强大,但体积也相当大。下载网站:http://ini4j.sourceforge.net/ 下面的例子用java.utils.map来存取ini。

3. 原始方法。 不需要使用任何外部类。下面代码转自他处。

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配置文档
 
*/
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("]");
 m = p.matcher((strLine));
 if (m.matches()) {
 p = Pattern.compile("]");
 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("]");
 m = p.matcher((strLine));
 //System.out.println("+++++++ ");
 if (m.matches()) {
 System.out.println("+++++++ ");
 p = Pattern.compile("]");
 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());
 }
 }
}

 

标签:trim,文件,java,String,strArray,value,ini,strLine
来源: https://www.cnblogs.com/longchang/p/11378648.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有