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("test.ini",false);
hpconfig.create();
String svalue=hpconfig.getValue("[Options]","API:PORT");
showDebug("[Options] API:PORT="+svalue);
svalue = "testofapiport";
hpconfig.setValue("[Options]","API:PORT",svalue);
hpconfig.setValue("[Options]","API:PORT2",svalue);
hpconfig.setValue("[Options]","API:PORT3",svalue);
hpconfig.setValue("[Options]","API:PORT4",svalue);
hpconfig.setValue("[Options]","API:PORT5",svalue);
//hpconfig.deleteValue("[Options2]","TEST0");
//hpconfig.deleteSection("[testsection]");
hpconfig.save();
showDebug("program end");
}
catch(Exception e)
{
//showError(e.toString());
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    class Section extends Object
    {
    public Section()
    {
    init();

    }
    public Section(String s)
    {
    setName(s);
    init();
    }

    private void init()
    {
    setPos(-1,-1);
    setName("");
    }

    private String sName;

    public String getName()
    {
    return sName;
    }
    public void setName(String s)
    {
    if(s==null)
    sName = "";
    else
    sName = s;
    }
    private int nStartLine;
    private int nEndLine;
    public void setPos(int start,int nend)
    {
    nStartLine = start;
    nEndLine = nend;
    }

    public boolean isExisted()
    {
    return nStartLine!=-1;
    }

    public void setValue(String valuename,String value,LinkedList lst)
    {
    int nbegin = nStartLine;
    int nend = nEndLine;
    if(nend<nbegin)
    nend = nbegin;
    String value_prefix = valuename+"=";
    String newline = valuename+"=" + value;
    for(int i=nbegin;i<=nend;i++)
    {
    String line = (String)lst.get(i);
    if(line.startsWith(value_prefix))
    {
    //replace the value
    lst.set(i,newline);
    return;
    }
    }
    //the value does not exist, insert it to the section end
    lst.add(nend,newline);
    nend++;
    }

    public String getValue(String valuename,LinkedList lst)
    {
    if(isExisted())
    {
    int nbegin = nStartLine;
    int nend = nEndLine;
    if(nend<=nbegin)
    return "";
    String value_prefix = valuename+"=";
    for(int i=nbegin;i<=nend;i++)
    {
    String line = (String)lst.get(i);
    if(line.startsWith(value_prefix))
    {
    return line.substring(value_prefix.length());
    }
    }

    }
    return "";
    }

    public void deleteValue(String valuename,LinkedList lst)
    {
    if(isExisted()==false)
    {
    showDebug("314,section does not exist");
    return;
    }
    int nbegin = nStartLine;
    int nend = nEndLine;
    if(nend<=nbegin)
    {
    showDebug("321,section is void");
    return;
    }
    String value_prefix = valuename+"=";
    for(int i=nbegin;i<=nend;i++)
    {
    String line = (String)lst.get(i);
    if(line.startsWith(value_prefix))
    {
    lst.remove(i);
    showDebug("331,delete value at line:"+i);
    return;
    }
    }
    showDebug("335,"+valuename +" is not found in section "+getName());


    public void delete(LinkedList lst)
    {
    if(isExisted()==false)
    {
    showDebug("363,deleteSection("+getName()+") does not exist");
    return;
    }
    for(int i=nEndLine;i>=nStartLine;i--)
    lst.remove(i);

    }

    private static void showError(String s)
    {
    System.out.println("Section:"+s);
    }

    private static void showDebug(String s)
    {
    System.out.println("Section:"+s);
    }

    public String toString()
    {
    return "startline:"+nStartLine+",end line:"+nEndLine;
    }
    }
      

  2.   

    对于INI文件Properties比较简便    try{
          InputStream ins = new FileInputStream("DBConfig.ini");
          Properties pr = new Properties();
          pr.load(ins);
          String dbname = pr.getProperty("DataBaseName","").trim();
          String ip = pr.getProperty("IPAddress","localhost").trim();
          user = pr.getProperty("username","").trim();
          passwd = pr.getProperty("password","").trim();
          if(!"".equals(dbname)){
            url = "jdbc:weblogic:mssqlserver4:"+dbname+"@"+ip+":1433";
          }
          return true;
        }catch(IOException ioe){
          return false;
        }
      

  3.   

    请问这个getProperty("username","")中的username和所要的值是用什么分割开的?“=”?