背景
公司老板希望导出单个客户资料,以前看过ini格式的内容输出,优点整洁、能清晰看到内容,所以当场就决定通过该方式输出,老板看到后挺满意的
1.展示效果
2.调用方式
public static void main(String[] args) throws IOException {
File file = new File("d:\\a.ini");
if(!file.exists())file.createNewFile();
//写入功能
write(file.getAbsolutePath(),"基本信息","姓名","张三");
write(file.getAbsolutePath(),"基本信息","性别","男");
write(file.getAbsolutePath(),"基本信息","年龄","18");
write(file.getAbsolutePath(),"基本信息","生日","1998-08-18");
write(file.getAbsolutePath(),"基本信息","家长","张帅");
//读取并输出到控制台
System.out.println(read(file.getAbsolutePath(),"基本信息","生日"));
}
3.工具类源码
package com.yanping;
import org.apache.commons.lang.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class IniUtils {
public static void main(String[] args) throws IOException {
File file = new File("d:\\a.ini");
if(!file.exists())file.createNewFile();
//写入功能
write(file.getAbsolutePath(),"基本信息","姓名","张三");
write(file.getAbsolutePath(),"基本信息","性别","男");
write(file.getAbsolutePath(),"基本信息","年龄","18");
write(file.getAbsolutePath(),"基本信息","生日","1998-08-18");
write(file.getAbsolutePath(),"基本信息","家长","张帅");
//读取并输出到控制台
System.out.println(read(file.getAbsolutePath(),"基本信息","生日"));
}
/**
* 根据section及key值读取ini文件的值
* @param filename ini文件名
* @param section 块名称:eg 读取[基本信息],只需传入"基本信息"即可
* @param key
* @return key对应的value
* @throws IOException
*/
public static String read(String filename,String section,String key) throws IOException {
return read(filename,section,key,null);
}
/**
* 根据section及key值读取ini文件的值
* @param filename ini文件名
* @param section 块名称:eg 读取[基本信息],只需传入"基本信息"即可
* @param key
* @param defaultValue 当配置文件中不存在key时 返回的默认值
* @return key对应的value
* @throws IOException
*/
public static String read(String filename,String section,String key,String defaultValue) throws IOException {
String value = null;
File file = FileUtils.getFile(filename);
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
String line = null;
String sectionLineRegex = "\\[.*\\]",sectionRegex = String.format("\\[%s\\]",section);
String keyLineRegex = String.format("%s\\s*=\\s*.*",key),keyRegex = String.format("%s\\s*=\\s*",key);
boolean active=false;
while ((line=bufferedReader.readLine())!=null){
line = StringUtils.trimToEmpty(line);
if(line.matches(sectionLineRegex)){
if(line.matches(sectionRegex)){
active=true;
}else{
active=false;
}
}
if(active&&line.matches(keyLineRegex)){
value = line.replaceFirst(keyRegex,"");
break;
}
}
return value==null?defaultValue:value;
}
}
/**
* 写入配置
* @param filename 文件名称
* @param section 模块名称
* @param key 键
* @param value 值
* @throws IOException
*/
public static void write(String filename,String section,String key,String value) throws IOException {
List<String> lines = getLines(filename);
int sectionIndex = hasSection(lines,section);
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filename))) {
if (sectionIndex>-1) {
Integer[] integers = getKeyIndex(lines,section,key);
int size = lines.size();
if (integers == null||integers.length==0) {
int addIndex=getWriteIndex(lines,section);//sectionIndex+1;
if(addIndex>=size){
lines.add(String.format("%s=%s", key, value));
}else{
lines.add(addIndex,String.format("%s=%s", key, value));
}
} else {
String line = null;
String valueRegex = "=.*";
for (Integer index:integers) {
line = lines.remove(index.intValue());
line = line.replaceFirst(valueRegex,String.format("=%s",value));
lines.add(index.intValue(),line);
}
}
} else {
int size = lines.size();
lines.add(size++,String.format("[%s]", section));
lines.add(size++,String.format("%s=%s", key, value));
}
bufferedWriter.write(lines.get(0));
for (int i = 1; i < lines.size(); i++) {
bufferedWriter.newLine();
bufferedWriter.write(lines.get(i));
}
}
}
/**
* 获取配置文件中所有行
* @param filename
* @return
* @throws IOException
*/
private static List<String> getLines(String filename) throws IOException {
List<String> lines = new ArrayList<>();
File file = FileUtils.getFile(filename);
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
String line = null;
while ((line=bufferedReader.readLine())!=null){
lines.add(StringUtils.trimToEmpty(line));
}
return lines;
}
}
/**
* 查看配置文件中是否存在section模块
* @param lines
* @param section
* @return -1或模块第一个所在行下标
* @throws IOException
*/
private static int hasSection(List<String> lines,String section) throws IOException {
String sectionLineRegex = "\\[.*\\]", sectionRegex = String.format("\\[%s\\]", section);
int sectionIndex = -1, currentIndex = 0;
for (String line:lines) {
line = StringUtils.trimToEmpty(line);
if (line.matches(sectionLineRegex)) {
if (line.matches(sectionRegex)) {
sectionIndex = currentIndex;
break;
}
}
currentIndex++;
}
return sectionIndex;
}
/**
* 获取key值的行下标
* @param lines
* @param section
* @param key
* @return
* @throws IOException
*/
private static Integer[] getKeyIndex(List<String> lines, String section, String key) throws IOException {
List<Integer> keyIndex = new ArrayList<>();
String value = null;
int index = 0, currentIndex = 0;
String sectionLineRegex = "\\[.*\\]", sectionRegex = String.format("\\[%s\\]", section);
String keyLineRegex = String.format("%s\\s*=\\s*.*", key), keyRegex = String.format("%s\\s*=\\s*", key);
boolean active = false;
for (String line:lines) {
if (line.matches(sectionLineRegex)) {
if (line.matches(sectionRegex)) {
active = true;
} else {
active = false;
}
}
if (active && line.matches(keyLineRegex)) {
index = currentIndex;
keyIndex.add(index);
}
currentIndex++;
}
Integer[] ints = new Integer[keyIndex.size()];
keyIndex.toArray(ints);
return ints;
}
/**
* 获取写入行下标
* @param lines
* @param section
* @return
*/
private static int getWriteIndex(List<String> lines,String section){
int writeIndex = -1;
boolean active = false;
String sectionLineRegex = "\\[.*\\]",sectionRegex = String.format("\\[%s\\]", section);
for (int i=0;i<lines.size();i++) {
if(lines.get(i).matches(sectionRegex)){
active=true;
}else if(active&&lines.get(i).matches(sectionRegex)){
writeIndex = i;
break;
}
}
return writeIndex==-1?lines.size():writeIndex;
}
}