根据网上其他代码进行修改。
读取文件:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class IniReader {
protected Map<String, Map<String, String>> sections = new LinkedHashMap<String, Map<String, String>>();
private transient String currentSecion;
private transient Map<String, String> current;
private String[] notes = new String[]{";", "#", "//"};
public IniReader(String filename) throws IOException {
BufferedReader reader = null;
try {
File file = new File(filename);
if(file.exists() && file.isFile()){
reader = new BufferedReader(new FileReader(file));
read(reader);
reader.close();
}
}finally{
if(reader != null){
reader.close();
}
}
}
protected void read(BufferedReader reader) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
parseLine(line);
}
}
protected void parseLine(String line) {
line = line.trim();
if (line.matches("\\[.*\\]")) {
currentSecion = line.replaceFirst("\\[(.*)\\]", "$1");
current = new LinkedHashMap<String, String>();
sections.put(currentSecion.trim(), current);
} else if (line.matches(".*=.*") && !line.startsWith("#")) {
if (current != null) {
//去掉注释
for(String str : notes){
int num = line.indexOf(str);
if(num != -1){
line = line.substring(0, num);
}
}
if(line.length() > 0 && line.indexOf("=") != -1){
String[] kv = line.split("=", 2);
current.put(kv[0].trim(), kv[1].trim());
}
}
}
}
public String getValue(String section, String name) {
Map<String, String> sectionmap = sections.get(section);
if (sectionmap == null) {
return null;
}
String value = sectionmap.get(name);
return value;
}
public Set<String> sectionKeys(){
return sections.keySet();
}
public Map<String, String> getValues(String section){
return sections.get(section);
}
public boolean containsKey(String section, String key) {
Map<String, String> m = sections.get(section);
if(m != null){
return m.get(key) == null;
}
return false;
}
public Map<String, Map<String, String>> getSections(){
return sections;
}
// public static void main(String[] args) {
// try {
// IniReader r = new IniReader("C:/Users/dell/Desktop/aa.ini");
String val = r.getValue("SET", "DBNAME");
// Set<String> keys = r.sectionKeys();
// for(String k : keys){
// System.out.println("[" + k + "]");
// Map<String, String> p = r.getValues(k);
// for(Map.Entry<String, String> entry : p.entrySet()){
// System.out.println(entry.getKey() + "==" + entry.getValue());
// }
// }
//
// Map<String, Map<String, String>> sections = r.getSections();
// new IniWriter("C:/Users/dell/Desktop/bb.ini", sections);
//
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}
写文件:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
public class IniWriter {
private StringBuffer sb = new StringBuffer();
public static String LINE_SEPARATOR = System.getProperty("line.separator");
public IniWriter(String filename, Map<String, Map<String, String>> sections) throws IOException {
BufferedWriter writer = null;
try {
writer = initWriter(filename);
initIni(sections);
writer.write(sb.toString());
} finally{
if(writer != null){
writer.close();
}
}
}
//初始化流
private BufferedWriter initWriter(String filename) throws IOException{
File file = new File(filename);
BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(file, false));
return writer;
}
//组装ini文件
private void initIni(Map<String, Map<String, String>> sections){
for(Map.Entry<String, Map<String, String>> e : sections.entrySet()){
String k = e.getKey();
Map<String, String> values = e.getValue();
sb.append("[" + k + "]");
sb.append(LINE_SEPARATOR);
for(Map.Entry<String, String> ev : values.entrySet()){
sb.append(ev.getKey() + "=" + ev.getValue());
sb.append(LINE_SEPARATOR);
}
}
}
}