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 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
Comment