read write INI file import java.io.*; import java.util.*;public class IniFileHandler { private LinkedList lstContentLine; public IniFileHandler() { init(); }
public IniFileHandler(String filepath,boolean bcreate) { sFilePath = filepath; init(); }
private void init() { //used in the constructor lstContentLine = new LinkedList(); }
public void create() throws Exception { //read the ini file into this object File f = new File(getFilePath()); FileInputStream fis = new FileInputStream(f); DataInputStream di = new DataInputStream(fis); String line = di.readLine(); while(line!=null) { line = trimLine(line); if(line!=null) lstContentLine.add(line); line = di.readLine(); } di.close();
}
private String trimLine(String s) { //remove leading spaces of the line //remove spaces before "=" if(s==null) return s; String s_raw = s; s = s.trim(); if(s==null) return s_raw; int npos=s.indexOf("="); if(npos<=0) return s; String sleft = s.substring(0,npos); //showDebug("56,"+sleft); int nleftlen = sleft.length()-1; int spacepos= nleftlen; while(sleft.charAt(spacepos)==' ') spacepos--; if(spacepos ==nleftlen)//no space before "=",return the original line return s; String sright = s.substring(npos); return sleft.substring(0,spacepos+1)+sright; }
private String getFilePath() { return sFilePath; } private String sFilePath; public void save() throws Exception { //save the ini saveAs(sFilePath); } public void saveAs(String saveaspath) throws Exception { if(saveaspath==null) { showError("22,path is null in saveAs()"); return ; } File fsave=new File(saveaspath); if(fsave.exists()==false) fsave.createNewFile(); FileOutputStream fos = new FileOutputStream(fsave); StringBuffer sbtmp=new StringBuffer(); int line_num = lstContentLine.size(); String line_separator = System.getProperty("line.separator"); for(int i=0;i<line_num;i++) { String line = (String)lstContentLine.get(i); sbtmp.append(line); sbtmp.append(line_separator); } fos.write(sbtmp.toString().getBytes()); fos.flush(); fos.close();
private Section findSection(String secnameraw) { String secname = formatSectionName(secnameraw); Section sectionobj = new Section(); int find_line_pos=-1; int end_line_pos = -1; int line_num = lstContentLine.size(); for(int i=0;i<line_num;i++) { String s = (String)lstContentLine.get(i); if(s.compareToIgnoreCase(secname)==0) { find_line_pos = i; break; } } //find section end if(find_line_pos>=0) end_line_pos = findLine("[",find_line_pos+1); if(end_line_pos<0) end_line_pos = lstContentLine.size()-1; if(end_line_pos<0) end_line_pos = find_line_pos; sectionobj.setPos(find_line_pos,end_line_pos); sectionobj.setName(secname); return sectionobj; }
private int findLine(String startline, int startfrom) { //find the first line starts with "startline" passed in int find_line_pos=-1; int line_num = lstContentLine.size(); if(startfrom >=line_num) return -1; for(int i=startfrom;i<line_num;i++) { String s = (String)lstContentLine.get(i); if(s.startsWith(startline)) { find_line_pos = i; break; } } return find_line_pos; }
import java.io.*; import java.util.*;public class IniFileHandler { private LinkedList lstContentLine; public IniFileHandler() { init(); }
public IniFileHandler(String filepath,boolean bcreate) { sFilePath = filepath; init(); }
private void init() { //used in the constructor lstContentLine = new LinkedList(); }
public void create() throws Exception { //read the ini file into this object File f = new File(getFilePath()); FileInputStream fis = new FileInputStream(f); DataInputStream di = new DataInputStream(fis); String line = di.readLine(); while(line!=null) { line = trimLine(line); if(line!=null) lstContentLine.add(line); line = di.readLine(); } di.close();
}
private String trimLine(String s) { //remove leading spaces of the line //remove spaces before "=" if(s==null) return s; String s_raw = s; s = s.trim(); if(s==null) return s_raw; int npos=s.indexOf("="); if(npos<=0) return s; String sleft = s.substring(0,npos); //showDebug("56,"+sleft); int nleftlen = sleft.length()-1; int spacepos= nleftlen; while(sleft.charAt(spacepos)==' ') spacepos--; if(spacepos ==nleftlen)//no space before "=",return the original line return s; String sright = s.substring(npos); return sleft.substring(0,spacepos+1)+sright; }
private String getFilePath() { return sFilePath; } private String sFilePath; public void save() throws Exception { //save the ini saveAs(sFilePath); } public void saveAs(String saveaspath) throws Exception { if(saveaspath==null) { showError("22,path is null in saveAs()"); return ; } File fsave=new File(saveaspath); if(fsave.exists()==false) fsave.createNewFile(); FileOutputStream fos = new FileOutputStream(fsave); StringBuffer sbtmp=new StringBuffer(); int line_num = lstContentLine.size(); String line_separator = System.getProperty("line.separator"); for(int i=0;i<line_num;i++) { String line = (String)lstContentLine.get(i); sbtmp.append(line); sbtmp.append(line_separator); } fos.write(sbtmp.toString().getBytes()); fos.flush(); fos.close();
private Section findSection(String secnameraw) { String secname = formatSectionName(secnameraw); Section sectionobj = new Section(); int find_line_pos=-1; int end_line_pos = -1; int line_num = lstContentLine.size(); for(int i=0;i<line_num;i++) { String s = (String)lstContentLine.get(i); if(s.compareToIgnoreCase(secname)==0) { find_line_pos = i; break; } } //find section end if(find_line_pos>=0) end_line_pos = findLine("[",find_line_pos+1); if(end_line_pos<0) end_line_pos = lstContentLine.size()-1; if(end_line_pos<0) end_line_pos = find_line_pos; sectionobj.setPos(find_line_pos,end_line_pos); sectionobj.setName(secname); return sectionobj; }
private int findLine(String startline, int startfrom) { //find the first line starts with "startline" passed in int find_line_pos=-1; int line_num = lstContentLine.size(); if(startfrom >=line_num) return -1; for(int i=startfrom;i<line_num;i++) { String s = (String)lstContentLine.get(i); if(s.startsWith(startline)) { find_line_pos = i; break; } } return find_line_pos; }
import java.io.*;
import java.util.*;public class IniFileHandler
{
private LinkedList lstContentLine;
public IniFileHandler()
{
init();
}
public IniFileHandler(String filepath,boolean bcreate)
{
sFilePath = filepath;
init();
}
private void init()
{
//used in the constructor
lstContentLine = new LinkedList();
}
public void create() throws Exception
{
//read the ini file into this object
File f = new File(getFilePath());
FileInputStream fis = new FileInputStream(f);
DataInputStream di = new DataInputStream(fis);
String line = di.readLine();
while(line!=null)
{
line = trimLine(line);
if(line!=null)
lstContentLine.add(line);
line = di.readLine();
}
di.close();
}
private String trimLine(String s)
{
//remove leading spaces of the line
//remove spaces before "="
if(s==null)
return s;
String s_raw = s;
s = s.trim();
if(s==null)
return s_raw;
int npos=s.indexOf("=");
if(npos<=0)
return s;
String sleft = s.substring(0,npos); //showDebug("56,"+sleft);
int nleftlen = sleft.length()-1;
int spacepos= nleftlen;
while(sleft.charAt(spacepos)==' ')
spacepos--;
if(spacepos ==nleftlen)//no space before "=",return the original line
return s;
String sright = s.substring(npos);
return sleft.substring(0,spacepos+1)+sright;
}
private String getFilePath()
{
return sFilePath;
}
private String sFilePath;
public void save() throws Exception
{
//save the ini
saveAs(sFilePath);
}
public void saveAs(String saveaspath) throws Exception
{
if(saveaspath==null)
{
showError("22,path is null in saveAs()");
return ;
}
File fsave=new File(saveaspath);
if(fsave.exists()==false)
fsave.createNewFile();
FileOutputStream fos = new FileOutputStream(fsave);
StringBuffer sbtmp=new StringBuffer();
int line_num = lstContentLine.size();
String line_separator = System.getProperty("line.separator");
for(int i=0;i<line_num;i++)
{
String line = (String)lstContentLine.get(i);
sbtmp.append(line);
sbtmp.append(line_separator);
}
fos.write(sbtmp.toString().getBytes());
fos.flush();
fos.close();
}
public String getValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
return cursect.getValue(valuename,lstContentLine);
}
public void setValue(String sectionname,String valuename, String value)
{
//if the section does not exist, create it.
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
cursect = createSection(sectionname);
} //showDebug("116,"+cursect.toString());
cursect.setValue(valuename,value,lstContentLine);
}
public void deleteValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
showDebug("125,"+sectionname+" does not exist");
}
cursect.deleteValue(valuename,lstContentLine); //showDebug("126,"+sectionname+" deleted");
}
private Section createSection(String sectionname)
{
String section_name = formatSectionName(sectionname);
lstContentLine.addLast(section_name);
int lines = lstContentLine.size()-1;
Section sectionobj = new Section();
sectionobj.setName(section_name);
sectionobj.setPos(lines,lines);
return sectionobj;
}
public void deleteSection(String sectionname)
{
Section sectionobj = findSection(sectionname);
if(sectionobj.isExisted()==false)
{
showDebug("145,deleteSection("+sectionobj.getName()+") does not exist");
return;
}
sectionobj.delete(lstContentLine);
}
private String formatSectionName(String sectionname)
{
//return "[section_name]"
String section_name;
if(sectionname.startsWith("["))
section_name = sectionname;
else
section_name = "["+sectionname+"]";
return section_name;
}
private Section findSection(String secnameraw)
{
String secname = formatSectionName(secnameraw);
Section sectionobj = new Section();
int find_line_pos=-1;
int end_line_pos = -1;
int line_num = lstContentLine.size();
for(int i=0;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.compareToIgnoreCase(secname)==0)
{
find_line_pos = i;
break;
}
}
//find section end
if(find_line_pos>=0)
end_line_pos = findLine("[",find_line_pos+1);
if(end_line_pos<0)
end_line_pos = lstContentLine.size()-1;
if(end_line_pos<0)
end_line_pos = find_line_pos;
sectionobj.setPos(find_line_pos,end_line_pos);
sectionobj.setName(secname);
return sectionobj;
}
private int findLine(String startline, int startfrom)
{
//find the first line starts with "startline" passed in
int find_line_pos=-1;
int line_num = lstContentLine.size();
if(startfrom >=line_num)
return -1;
for(int i=startfrom;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.startsWith(startline))
{
find_line_pos = i;
break;
}
}
return find_line_pos;
}
private static void showError(String s)
{
System.out.println("IniFileHandler:"+s);
}
private static void showDebug(String s)
{
System.out.println("IniFileHandler:"+s);
}
public static void main(String args[])
{
//open hpconfig.ini
try
{
IniFileHandler hpconfig=new IniFileHandler("hpconfig1.ini",false);
hpconfig.create();
String svalue=hpconfig.getValue("[SECTION1]","API:PORT");
showDebug("[SECTION1] API:PORT="+svalue);
svalue = "testofapiport";
hpconfig.setValue("[SECTION1]","API:PORT",svalue);
hpconfig.setValue("[SECTION1]","API:PORT2",svalue);
hpconfig.setValue("[SECTION1]","API:PORT3",svalue);
hpconfig.setValue("[SECTION1]","API:PORT4",svalue);
hpconfig.setValue("[SECTION1]","API:PORT5",svalue);
//hpconfig.deleteValue("[Disk Options]","TEST0");
//hpconfig.deleteSection("[testsection]");
hpconfig.save();
showDebug("program end");
}
catch(Exception e)
{
//showError(e.toString());
e.printStackTrace();
}
}
}
import java.util.*;public class IniFileHandler
{
private LinkedList lstContentLine;
public IniFileHandler()
{
init();
}
public IniFileHandler(String filepath,boolean bcreate)
{
sFilePath = filepath;
init();
}
private void init()
{
//used in the constructor
lstContentLine = new LinkedList();
}
public void create() throws Exception
{
//read the ini file into this object
File f = new File(getFilePath());
FileInputStream fis = new FileInputStream(f);
DataInputStream di = new DataInputStream(fis);
String line = di.readLine();
while(line!=null)
{
line = trimLine(line);
if(line!=null)
lstContentLine.add(line);
line = di.readLine();
}
di.close();
}
private String trimLine(String s)
{
//remove leading spaces of the line
//remove spaces before "="
if(s==null)
return s;
String s_raw = s;
s = s.trim();
if(s==null)
return s_raw;
int npos=s.indexOf("=");
if(npos<=0)
return s;
String sleft = s.substring(0,npos); //showDebug("56,"+sleft);
int nleftlen = sleft.length()-1;
int spacepos= nleftlen;
while(sleft.charAt(spacepos)==' ')
spacepos--;
if(spacepos ==nleftlen)//no space before "=",return the original line
return s;
String sright = s.substring(npos);
return sleft.substring(0,spacepos+1)+sright;
}
private String getFilePath()
{
return sFilePath;
}
private String sFilePath;
public void save() throws Exception
{
//save the ini
saveAs(sFilePath);
}
public void saveAs(String saveaspath) throws Exception
{
if(saveaspath==null)
{
showError("22,path is null in saveAs()");
return ;
}
File fsave=new File(saveaspath);
if(fsave.exists()==false)
fsave.createNewFile();
FileOutputStream fos = new FileOutputStream(fsave);
StringBuffer sbtmp=new StringBuffer();
int line_num = lstContentLine.size();
String line_separator = System.getProperty("line.separator");
for(int i=0;i<line_num;i++)
{
String line = (String)lstContentLine.get(i);
sbtmp.append(line);
sbtmp.append(line_separator);
}
fos.write(sbtmp.toString().getBytes());
fos.flush();
fos.close();
}
public String getValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
return cursect.getValue(valuename,lstContentLine);
}
public void setValue(String sectionname,String valuename, String value)
{
//if the section does not exist, create it.
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
cursect = createSection(sectionname);
} //showDebug("116,"+cursect.toString());
cursect.setValue(valuename,value,lstContentLine);
}
public void deleteValue(String sectionname,String valuename)
{
Section cursect = findSection(sectionname);
if(cursect.isExisted()==false)
{
showDebug("125,"+sectionname+" does not exist");
}
cursect.deleteValue(valuename,lstContentLine); //showDebug("126,"+sectionname+" deleted");
}
private Section createSection(String sectionname)
{
String section_name = formatSectionName(sectionname);
lstContentLine.addLast(section_name);
int lines = lstContentLine.size()-1;
Section sectionobj = new Section();
sectionobj.setName(section_name);
sectionobj.setPos(lines,lines);
return sectionobj;
}
public void deleteSection(String sectionname)
{
Section sectionobj = findSection(sectionname);
if(sectionobj.isExisted()==false)
{
showDebug("145,deleteSection("+sectionobj.getName()+") does not exist");
return;
}
sectionobj.delete(lstContentLine);
}
private String formatSectionName(String sectionname)
{
//return "[section_name]"
String section_name;
if(sectionname.startsWith("["))
section_name = sectionname;
else
section_name = "["+sectionname+"]";
return section_name;
}
private Section findSection(String secnameraw)
{
String secname = formatSectionName(secnameraw);
Section sectionobj = new Section();
int find_line_pos=-1;
int end_line_pos = -1;
int line_num = lstContentLine.size();
for(int i=0;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.compareToIgnoreCase(secname)==0)
{
find_line_pos = i;
break;
}
}
//find section end
if(find_line_pos>=0)
end_line_pos = findLine("[",find_line_pos+1);
if(end_line_pos<0)
end_line_pos = lstContentLine.size()-1;
if(end_line_pos<0)
end_line_pos = find_line_pos;
sectionobj.setPos(find_line_pos,end_line_pos);
sectionobj.setName(secname);
return sectionobj;
}
private int findLine(String startline, int startfrom)
{
//find the first line starts with "startline" passed in
int find_line_pos=-1;
int line_num = lstContentLine.size();
if(startfrom >=line_num)
return -1;
for(int i=startfrom;i<line_num;i++)
{
String s = (String)lstContentLine.get(i);
if(s.startsWith(startline))
{
find_line_pos = i;
break;
}
}
return find_line_pos;
}
private static void showError(String s)
{
System.out.println("IniFileHandler:"+s);
}
private static void showDebug(String s)
{
System.out.println("IniFileHandler:"+s);
}
public static void main(String args[])
{
//open hpconfig.ini
try
{
IniFileHandler hpconfig=new IniFileHandler("hpconfig1.ini",false);
hpconfig.create();
String svalue=hpconfig.getValue("[SECTION1]","API:PORT");
showDebug("[SECTION1] API:PORT="+svalue);
svalue = "testofapiport";
hpconfig.setValue("[SECTION1]","API:PORT",svalue);
hpconfig.setValue("[SECTION1]","API:PORT2",svalue);
hpconfig.setValue("[SECTION1]","API:PORT3",svalue);
hpconfig.setValue("[SECTION1]","API:PORT4",svalue);
hpconfig.setValue("[SECTION1]","API:PORT5",svalue);
//hpconfig.deleteValue("[Disk Options]","TEST0");
//hpconfig.deleteSection("[testsection]");
hpconfig.save();
showDebug("program end");
}
catch(Exception e)
{
//showError(e.toString());
e.printStackTrace();
}
}
}
我想在配置文件里加上注释,因为东西很多,乱了以后太难维护了。
难道剩下的方法只有文件读写,一行一行把内容写进去?
我觉得这样的方法太难维护了,
各位有什么好的建议吗?
在写回去不就可以了吗?