String replacer not working ..can any one help me?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laksh2007
    New Member
    • Dec 2007
    • 3

    String replacer not working ..can any one help me?

    i want to search and replace strings present in a file (entire file)..
    i have wirtten cod eto do this..
    but it is not working as expected..

    code is :


    import java.io.*;
    import java.util.*;

    public class StringReplacer {

    private String replaceString;
    private String replacerString;
    private String filePath;

    public StringReplacer( String replaceString, String replacerString, String filePath)
    {
    this.replaceStr ing = replaceString;
    this.replacerSt ring = replacerString;
    this.filePath = filePath;
    try
    {
    replace();
    }
    catch (Exception ex)
    {
    }
    }

    public StringReplacer( )
    {

    }
    private void replace() throws Exception
    {
    //RandomAccessFil e raf=new RandomAccessFil e(filepath,"rwd ");
    File file = new File(filePath);
    if(!file.exists ())
    {
    System.out.prin tln("File doesnot exists");
    return;
    }

    DataInputStream dis = new DataInputStream (new FileInputStream (file));

    StringBuffer fileData = new StringBuffer("" );
    String strTemp = "";

    System.out.prin tln("before while");
    while((strTemp = dis.readLine()) != null)
    {
    //String str=new String();
    System.out.prin tln("inside while");
    fileData.append (strTemp);
    System.out.prin tln(strTemp);
    }
    System.out.prin tln("after while");


    dis.close();

    System.out.prin tln("before string tokenizer");
    StringTokenizer stTemp = new StringTokenizer (fileData.toStr ing()," ",true);
    System.out.prin tln(stTemp);
    System.out.prin tln("after string tokenizer");

    int tokens = stTemp.countTok ens();

    if(tokens <= 1)
    {
    // No matches found for string to be replaces.
    System.out.prin tln("No matches found for string to be replaced");
    return;
    }

    fileData = new StringBuffer("" );

    while(stTemp.ha sMoreTokens())
    {
    //fileData.append (stTemp.nextTok en());// + replacerString) ;
    fileData.append (stTemp.nextTok en() + replacerString) ;
    }

    if(file.exists( ))
    file.delete();

    file = new File(filePath);
    if(!file.exists ())
    file.createNewF ile();

    DataOutputStrea m dos = new DataOutputStrea m(new FileOutputStrea m(file));
    dos.writeChars( fileData.toStri ng());
    dos.flush();
    dos.close();
    }



    }

    when i run this ..

    i am not getting expected out put..
    please suggest me something
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by laksh2007
    i want to search and replace strings present in a file (entire file)..
    i have wirtten cod eto do this..
    but it is not working as expected..

    code is :


    import java.io.*;
    import java.util.*;

    public class StringReplacer {

    private String replaceString;
    private String replacerString;
    private String filePath;

    public StringReplacer( String replaceString, String replacerString, String filePath)
    {
    this.replaceStr ing = replaceString;
    this.replacerSt ring = replacerString;
    this.filePath = filePath;
    try
    {
    replace();
    }
    catch (Exception ex)
    {
    }
    }

    public StringReplacer( )
    {

    }
    private void replace() throws Exception
    {
    //RandomAccessFil e raf=new RandomAccessFil e(filepath,"rwd ");
    File file = new File(filePath);
    if(!file.exists ())
    {
    System.out.prin tln("File doesnot exists");
    return;
    }

    DataInputStream dis = new DataInputStream (new FileInputStream (file));

    StringBuffer fileData = new StringBuffer("" );
    String strTemp = "";

    System.out.prin tln("before while");
    while((strTemp = dis.readLine()) != null)
    {
    //String str=new String();
    System.out.prin tln("inside while");
    fileData.append (strTemp);
    System.out.prin tln(strTemp);
    }
    System.out.prin tln("after while");


    dis.close();

    System.out.prin tln("before string tokenizer");
    StringTokenizer stTemp = new StringTokenizer (fileData.toStr ing()," ",true);
    System.out.prin tln(stTemp);
    System.out.prin tln("after string tokenizer");

    int tokens = stTemp.countTok ens();

    if(tokens <= 1)
    {
    // No matches found for string to be replaces.
    System.out.prin tln("No matches found for string to be replaced");
    return;
    }

    fileData = new StringBuffer("" );

    while(stTemp.ha sMoreTokens())
    {
    //fileData.append (stTemp.nextTok en());// + replacerString) ;
    fileData.append (stTemp.nextTok en() + replacerString) ;
    }

    if(file.exists( ))
    file.delete();

    file = new File(filePath);
    if(!file.exists ())
    file.createNewF ile();

    DataOutputStrea m dos = new DataOutputStrea m(new FileOutputStrea m(file));
    dos.writeChars( fileData.toStri ng());
    dos.flush();
    dos.close();
    }



    }

    when i run this ..

    i am not getting expected out put..
    please suggest me something
    I think you use the Java 1.3.
    From Java 1.4 the String.replace method available.
    I think you try to write code yourself.
    Good Idea!
    First get the total string in a String variable.
    Then try my code.....

    [code=java]
    String file_str = ......
    if(file_str.ind exOf(replaceStr ing)!=-1){
    StringBuffer temp_str = new StringBuffer(fi le_str);
    boolean complete = false;
    int start_pos;
    while(!complete )
    if((start_pos=t emp_str.toStrin g().indexOf(rep laceString))!-=1) temp_str = temp_str.replac e(start_pos,rep lace_str.length (),replacerStri ng);
    String replaced_str = temp_str.toStri ng();
    }
    [/code]

    I think it will work.

    Debasis Jana

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Don't use DataStreams for text reading/writing purposes; use Readers and Writers
      instead. They're made for that job.

      kind regards,

      Jos

      Comment

      Working...