package com.iss.iaf.codemanagement.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 解析配置文档操作类,用来读取和配置ini配置文档
*
* @author xinzhangah
* @date 2016-12-08
*
*/
public class IniFile {
/**
* 解析ini文件,返回list中包含List的节点
*
* @param filePath
* 配置文件的路径
* @return 所有的节点和属性
* @author xinzhangah
* @date 2016-12-07
*/
public static List getFileSection(String filePath) {
List listAll = new ArrayList();
String strLine = "";
BufferedReader bufferedReader;
try {
FileInputStream fis = new FileInputStream(filePath);
bufferedReader = new BufferedReader(new InputStreamReader(fis,
"utf-8"));
while ((strLine = bufferedReader.readLine()) != null) {
strLine = strLine.trim();
// 此部分为注释
if (strLine.matches("^\\#.*$")) {
} else if (strLine.matches("^\\[\\S+\\]$")) {
// section
String section = strLine.replaceFirst("^\\[(\\S+)\\]$",
"$1");
// 将节点放入listAll中
listAll.add(section);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return listAll;
}
/**
* 解析ini文件,返回Map键值对的属性
*
* @param filePath
* 配置文件的路径
* @return 所有的节点和属性
* @author xinzhangah
* @date 2016-12-07
*/
public static List getFileKeyValue(String filePath) {
List listAll = new ArrayList();
String strLine = "";
BufferedReader bufferedReader;
try {
FileInputStream fis = new FileInputStream(filePath);
bufferedReader = new BufferedReader(new InputStreamReader(fis,
"utf-8"));
Map map = null;
while ((strLine = bufferedReader.readLine()) != null) {
strLine = strLine.trim();
// 此部分为注释
if (strLine.matches("^\\#.*$")) {
} else if (strLine.matches("^\\S+=.*$")) {
// key ,value
if (map != null) {
int i = strLine.indexOf("=");
String key = strLine.substring(0, i).trim();
String value = strLine.substring(i + 1).trim();
map.put(key, value);
}
} else if (strLine.matches("^\\[\\S+\\]$")) {
map = new HashMap();
listAll.add(map);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return listAll;
}
/**
* 修改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;
String getValue;
FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fis, "utf-8"));
boolean isInSection = false;
boolean isTrue = false;
fileContent = "";
try {
while ((allLine = bufferedReader.readLine()) != null) {
allLine = allLine.trim();
System.out.println("allLine:" + allLine);
strLine = allLine;
if (strLine.matches("^\\[\\S+\\]$")) { // 是节点
if (section.equals(strLine.substring(1,
strLine.length() - 1))) { // 是要修改的节点
isInSection = true;
}
} else if (strLine.matches("^\\S+=.*$")) { // 键值对
isTrue = true;
}
if (isInSection == true && isTrue == true) { // 键值对
strLine = strLine.trim();
String[] strArray = strLine.split("=");
getValue = strArray[0].trim();
if (getValue.equalsIgnoreCase(variable)) {
newLine = getValue + "=" + value + " ";
fileContent += newLine + "\r\n";
while ((allLine = bufferedReader.readLine()) != null) {
fileContent += allLine + "\r\n";
}
bufferedReader.close();
java.io.FileOutputStream writerStream = new java.io.FileOutputStream(
file);
BufferedWriter bufferedWriter = new BufferedWriter(
new java.io.OutputStreamWriter(writerStream,
"UTF-8"));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close();
return true;
}
}
fileContent += allLine + "\r\n";
}
} catch (IOException ex) {
throw ex;
} finally {
bufferedReader.close();
}
return false;
}
/**
*
* @param section
* 节的名字,是一个以0结束的字符串
* @param Key
* 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
* @param value
* 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
* @param filePath
* 要写入的文件的文件名。若该ini文件与程序在同一个目录下,也可使用相对路径,否则需要给出绝度路径。
* @return
*/
public static boolean WritePrivateProfileString(String section, String key,
String value, String filePath) {
boolean ret = false;
boolean sectionFlag = false; // 标识是否存在节点
// section是null则直接返回false
if (section == null) {
return ret;
}
String strSection = "[" + section + "]";
String strKey = key + "=" + value;
File file = new File(filePath);
if (!file.exists()) {//测试此抽象路径名表示的文件或目录是否存在。
try {
file.createNewFile(); //当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
} catch (IOException e) {
e.printStackTrace();
}
}
List list = getFileSection(filePath);// 获取节点list
if(list.size()>0){//如果存在节点,做判断
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(section)) {
sectionFlag = true; // 存在节点
}
}
}
FileWriter fileWritter;
BufferedWriter bufferWritter = null;
try {
fileWritter = new FileWriter(file,true);
bufferWritter = new BufferedWriter(fileWritter);
if (sectionFlag == false) { // 不存在节点时,将节点写入
bufferWritter.write(strSection);
}
bufferWritter.newLine();
bufferWritter.write(strKey);
ret = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferWritter != null) {
bufferWritter.flush();
bufferWritter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
}
}
联系客服