/**
 * 将一个字符替换成目标字符串
 * @param source 源字符串
 * @param src 字符
 * @param dst 目标字符串
 */
public static String Replace(String source,char src,String dst) {
StringBuffer sBuf = new StringBuffer(source.length()+(source.length()/20)*dst.length()+100);
int offset = 0;
int index = 0;
while(offset<source.length()&&(index = source.indexOf((int)src,offset))>0){
sBuf.append(source.substring(offset,index));
sBuf.append(dst);
offset=index+1;
}
if(offset<source.length())
sBuf.append(source.substring(offset));
return sBuf.toString();
}

解决方案 »

  1.   

    To c_crazyren(楚狂人):
                          可能我没有把我的问题说清楚,下面是我的程序。
    String path=application.getRealPath(".");
    RandomAccessFile rf=new RandomAccessFile(path+"\\dhcpd1.conf","rw");String temp="";
    String temp1="70000";
    int index0,index1;
    long pos;
    pos=rf.getFilePointer();
    String line=rf.readLine();
    while(line!=null)
    {
      if(line.indexOf("default-lease-time")!=-1)
      {
         index0=line.indexOf(" ");
         index1=line.indexOf(";");
         temp=line.substring(index0+1,index1);
         line=Jsp1BeanId.replace(line,temp,temp1);
         rf.seek(pos);
         rf.writeBytes(line);
        }
        pos=rf.getFilePointer();
        line=rf.readLine();
    }
    rf.close();
    在上面程序中我对文件dhcpd.conf1一行行读入变量line中,当遇到“default-lease-time”字符串时,我对其后面从空格到分号为止的字符串用变量temp1替换,然后回到这一行行首,将替换后的字符串写入文件,可替换后变量line长度如果改变,将其再写入文件中时,会破坏文件的其他部分啊!