突然想做一个类似KDE管理中心那样的gnome的配置管理器,来集中的管理gnome或其他图形管理环境下系统的配置。而众所周知,linux的底层配置多是用各种配置文件来保存信息的,有早期的::分割格式的,有新的xml分割的,还有一些特殊脚本形式的。其中尚有一些是采用的类似windows
ini配置文件格式的。
不知道是我固陋寡闻还是真的就没有,java
api里居然没有ini读写操作支持,查找一些文章,有的说什么Properties能操作ini,但我发现它好像是一个操作xml配置文件的类,总之不管如何,有也好没也好,ini文件结构并不复杂,自己写了一个完整的工具类,就当好久没弄java了练练手吧。
一下转入正题:
思路:ini配置文件-->数组-->ArrayList1-->清理代码(单行分析,先区分空行,注释行,替换为注释符,去掉首尾空格=两侧空格)-->ArrayList2(此后的读操作用2,写操作到1)-->查找类别-->查找变量-->得到值[-->改写值-->myWriter()写入文件]
(恕我不善言词,直接贴出代码)
package mypg;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*
* @className MyINI
* @author Hongs(xhacker008)
* @version 2005.11.28
*/
public class MyINI {
private String file = null;
private ArrayList inList = new ArrayList(); // 源列表
private ArrayList outList = new ArrayList();
private ArrayList tagList = new ArrayList();
public MyINI(){
tagList.clear();
}
public MyINI(String file) { // 构造函数
this.file = file;
try{
this.myReader();
this.myCopy();
} catch(IOException ioe) {};
tagList.clear();
}
public void myReader() throws IOException {
String myStr = null;
BufferedReader bufferedReader = new BufferedReader(new FileReader(new
File(this.file)));
int i = 0;
try {
while((myStr = bufferedReader.readLine()) != null){
this.inList.add(myStr.toString());
i = i+1;
}
} finally {
bufferedReader.close();
}
} //end myReader();
public void myWriter() throws IOException {
String temp = inList.get(0).toString();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
File(this.file)));
for(int i = 1;i < this.inList.size();i++) {
temp = temp + "\r\n" + inList.get(i).toString();
}
try {
bufferedWriter.write(temp);
} finally {
bufferedWriter.close();
};
} //end myWriter();
private void myCopy() { // make a new list and just use this list later;
String[] temp = {";",";"};
for(int i = 0; i < this.inList.size(); i ++) {
temp[0] = this.inList.get(i).toString().trim();
//System.out.println("#"+temp[0]);
if(temp[0].equals("")) {
this.outList.add(temp[1]+"?");
} else if(temp[0].matches("\\[\\s*.*\\s*\\]")) {
this.outList.add(temp[0]);//System.out.println("ok1");
} else if(temp[0].matches("[a-zA-Z0-9]+=+.*")) {
temp = this.inList.get(i).toString().split("=");
this.outList.add(temp[0].trim()+"="+temp[1].trim());//System.out.println("ok2");
} else {
this.outList.add(temp[1]);
}
}
}
public void myClear(){
tagList.clear();
}
//mySearch();
private int[] mySearch(String[] args) { // args = {section, variable}
int max = this.outList.size();
char ch;
String str;
int[] j = {-1, -1};
for(int i = 0; i < max; i ++) {
if(this.outList.get(i).toString().matches("\\[\\s*"+args[0]+"\\s*\\]")){ // 找到
section;
j[0] = i;
i = i + 1;
while((ch = (str = this.outList.get(i).toString()).toCharArray()[0]) != '[') {
//System.out.println("$"+str);
if(ch != ';'){
if(str.split("=")[0].equals(args[1])){ // 找到 variable;
j[1] = i;
break; // break out while();
} else if(i < max - 1) {
i = i + 1;
} else {
break; // break out while();
}
} else {
i = i + 1;
}
}
break;
}
}
return j;
}
public int[] mySearch(String section, String variable) {
String[] temp = {section, variable};
int[] j = this.mySearch(temp);
return j;
}
//end mySearchOne();
//myGetOne();
public String myGetOne(String section, String variable, String temp){
int[] j = this.mySearch(section, variable);
if(j[0] != -1){
if(j[1] != -1){
if((temp = this.outList.get(j[1]).toString().split("=")[1]) != ""){
return temp;
}
}
}
return temp;
}
public String myGetOne(String section, String variable){ // 2 parameters;
return this.myGetOne(section, variable, "?");
}
//end myGetOne();
public void mySetOne(String section, String variable, String value) throws
IOException {
int[] j = this.mySearch(section, variable);
if(j[0] != -1) {
if(j[1] != -1) {
this.inList.set(j[1], variable+"="+value);
}
else {
this.inList.add(j[0]+1, variable+"="+value);
}
}
else {
this.inList.add("["+section+"]"+"\r\n"+variable+"="+value);
}
} //end mySetOne();
/*
* 上边是对单行操作,
* 下边成批处理;
* @author Hongs(xhacker008)
* @version 2005.12.3
*/
//myAdd();
private void myAdd(String[] args){ // args = {section, variable [, value]};
this.tagList.add(args);
}
public void myAdd(String section, String variable, String value) {
String[] temp = {section, variable, value};
this.myAdd(temp);
}
public void myAdd(String section, String variable) {
String[] temp = {section, variable, "?"};
this.myAdd(temp);
}
public void myAdd(String section, String[][] arr) { // arr[i] = {variable, [,
value]};
int i = 0;
String[] temp = new String[2];
while(!arr[i][0].equals(null)){
for(int j = 0; j < 2; j++){
if(j == 0){
temp[j] = section;
} else {
temp[j] = arr[i][j-1];
}
this.myAdd(temp);
}
i = i + 1;
}
}
//end myAdd();
public String[] myGet() {
String[] value = new String[tagList.size()];
String[] temp = new String[3];
for(int i = 0; i < tagList.size(); i ++){
System.arraycopy(tagList.get(i), 0, temp, 0, 2);
value[i] = this.myGetOne(temp[0], temp[1]);
}
return value;
}
public void mySet() {
String[] temp = new String[3];
for(int i = 0; i < tagList.size(); i ++){
System.arraycopy(tagList.get(i), 0, temp, 0, 3);
try {
this.mySetOne(temp[0], temp[1], temp[2]);
} catch(IOException ioe){};
}
}
public ArrayList mySearchAll() {
ArrayList tag = new ArrayList();
String[] temp = new String[2];
for(int i = 0; i < tagList.size(); i ++){
System.arraycopy(tagList.get(i), 0, temp, 0, 2);
tag.add(this.mySearch(temp[0], temp[1]));
}
return tag;
}
}