filestream读进来为String
然后查找替换“[domain1]”为你所要替换的[User1=xbh]”,然后再输出保存example.txt就可以了啊

解决方案 »

  1.   

    读入内容,专成StringBuffer,定位indexOf,提换replace
      

  2.   

    这个问题我已经解决了,有关注的,我写一下解决方法。
    protected String fileName;      
    private static String filename= "d:\\project\\ServUDaemon.ini  ";      
    static File aFile = new File(filename);      
    private static Vector;         
    void jButton1_actionPerformed(ActionEvent e)    {      
         PrintWriter out = null;      
         BufferedReader in = null;      
         try{      
            in =new BufferedReader(new FileReader(aFile));      
         }catch (Exception    exx{      
            exx.printStackTrace(System.out);      
         }      
        vec=new Vector();
         try{      
            String s = in.readLine();      
            String original ="\\[Domain1\\]  ";  //被替换的文字      
          String temp="bbbbbbbb";
            String replacestr = original +replacestring ;     //待替换文字          
            File file=new File(filename);        //开始在文件末尾加入内容      
            BufferedWriter out1=new BufferedWriter(new FileWriter(file,true));      
            String appendstr="aaaaaaa  ";      
            out1.write(String.valueOf(appendstr));      
            out1.close();      
            out1=null;      
            file=null;                 //在文件末尾加入内容结束      
            while(s!=null){        //开始查找并替换内容      
             s=s.replaceAll(original,temp); 
              s=s.replaceAll(temp,replacestr);     
             vec.add(s);      
             s=in.readLine();      
           }      
          in.close();        
           try {      
             out=new PrintWriter(new FileWriter(aFile,false));      
            } catch (IOException ex) {      
            }      
            for (int i=0;i<vec.size();i++){      
               out.println((String)vec.get(i));      
               System.out.println((String)vec.get(i));      
            }      
           out.close();      
          }catch    (IOException    ex1)    {      
          }      
    } 这里有个问题:当while循环执行的时候,由于我待替换的字符串包含了待替换的字符串,那么它就不断不断地用待替换的字符串来替换新写入的“[Domain1]”,变成死循环,只能设一个临时的字符串,让临时字符串先替换“[Domain1]”,再把需要写入的待替换的文字把临时字符串tempf替换掉,就避免了死循环。