解决方案 »

  1.   

    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);
    }
      

  2.   

    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);
    }
      

  3.   

    public static void main(String args[])
    {
    //open myconfig.ini
    try
    {
    IniFileHandler myconfig=new IniFileHandler("myconfig1.ini",false);
    myconfig.create();
    String svalue=myconfig.getValue("[mysection1]","API:PORT");
    showDebug("[mysection1] API:PORT="+svalue);
    svalue = "testofapiport";
    myconfig.setValue("[mysection1]","API:PORT",svalue);
    myconfig.setValue("[mysection1]","API:PORT2",svalue);
    myconfig.setValue("[mysection1]","API:PORT3",svalue);
    myconfig.setValue("[mysection1]","API:PORT4",svalue);
    myconfig.setValue("[mysection1]","API:PORT5",svalue); myconfig.save();
    showDebug("program end");
    }
    catch(Exception e)
    {
    //showError(e.toString());
    e.printStackTrace();
    }
    }
    }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;
    }
    }
      

  4.   

    I am obliged to thank you for the trouble you have taken at this matter!
    thank you masterZ!
      

  5.   

    Hi superzrb
    can you tell me what's the meaning of "gz"?
      

  6.   

    使用什么配置文件啊,
    如果是XML的,Sax和Dom就可以。还挺方便。
      

  7.   

    请看我写的一篇文章:将应用程序的设定存在哪里? 
    http://www.csdn.net/Develop/read_article.asp?id=13885
      

  8.   

    呵呵,最简单的应该就是直接用Properties类了吧!可以直接load一个文件近来,格式就是xxx=xxx,然后getProperty("xx")就可以得到了,这是最简单的方法吧!
      

  9.   

    private void loadConfig("myfile.property") throws Exception
      // public void loadConfig() throws Exception
        {
       
         //配置
            FileInputStream file=new FileInputStream(fileName);
            pro = new Properties();
            pro.load(file);
    //取其中的参数
            String p_host,p_port,p_timeout,p_log;
            p_host=pro.getProperty("host").trim();
            p_port=pro.getProperty("port");
            p_timeout=pro.getProperty("timeout");
            ......
    }//myfile.property的参数配置形式
    host=130.130.10.18
    port=5080
    #port=1008
    timeout=100000
      

  10.   

    对不起上面有点小错误:
    FileInputStream file=new FileInputStream(fileName); 这个fileName本来是以参数传进来的,这里应该是FileInputStream file=new FileInputStream("myfile.property");
            
      

  11.   

    MasterZ,morning!Warning Message when I compile the code Warning #: 370 : Deprecated APIs are used. Please recompile with the "deprecation" option for detailsI am a beginner on Java and EJB ,May I have your Instruction?
      

  12.   

    the class is used to access windows ini file(xxx.ini). that warning does not affect the function of the code. you can ignore it.
      

  13.   

    I coded a IniClass yesterday.In this file,Hashtable is used