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.replaceString = replaceString;
this.replacerString = replacerString;
this.filePath = filePath;
try
{
replace();
}
catch (Exception ex)
{
}
}
public StringReplacer()
{
}
private void replace() throws Exception
{
//RandomAccessFile raf=new RandomAccessFile(filepath,"rwd");
File file = new File(filePath);
if(!file.exists())
{
System.out.println("File doesnot exists");
return;
}
DataInputStream dis = new DataInputStream(new FileInputStream(file));
StringBuffer fileData = new StringBuffer("");
String strTemp = "";
System.out.println("before while");
while((strTemp = dis.readLine()) != null)
{
//String str=new String();
System.out.println("inside while");
fileData.append(strTemp);
System.out.println(strTemp);
}
System.out.println("after while");
dis.close();
System.out.println("before string tokenizer");
StringTokenizer stTemp = new StringTokenizer(fileData.toString()," ",true);
System.out.println(stTemp);
System.out.println("after string tokenizer");
int tokens = stTemp.countTokens();
if(tokens <= 1)
{
// No matches found for string to be replaces.
System.out.println("No matches found for string to be replaced");
return;
}
fileData = new StringBuffer("");
while(stTemp.hasMoreTokens())
{
//fileData.append(stTemp.nextToken());// + replacerString);
fileData.append(stTemp.nextToken() + replacerString);
}
if(file.exists())
file.delete();
file = new File(filePath);
if(!file.exists())
file.createNewFile();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeChars(fileData.toString());
dos.flush();
dos.close();
}
}
when i run this ..
i am not getting expected out put..
please suggest me something
2 1519
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.replaceString = replaceString;
this.replacerString = replacerString;
this.filePath = filePath;
try
{
replace();
}
catch (Exception ex)
{
}
}
public StringReplacer()
{
}
private void replace() throws Exception
{
//RandomAccessFile raf=new RandomAccessFile(filepath,"rwd");
File file = new File(filePath);
if(!file.exists())
{
System.out.println("File doesnot exists");
return;
}
DataInputStream dis = new DataInputStream(new FileInputStream(file));
StringBuffer fileData = new StringBuffer("");
String strTemp = "";
System.out.println("before while");
while((strTemp = dis.readLine()) != null)
{
//String str=new String();
System.out.println("inside while");
fileData.append(strTemp);
System.out.println(strTemp);
}
System.out.println("after while");
dis.close();
System.out.println("before string tokenizer");
StringTokenizer stTemp = new StringTokenizer(fileData.toString()," ",true);
System.out.println(stTemp);
System.out.println("after string tokenizer");
int tokens = stTemp.countTokens();
if(tokens <= 1)
{
// No matches found for string to be replaces.
System.out.println("No matches found for string to be replaced");
return;
}
fileData = new StringBuffer("");
while(stTemp.hasMoreTokens())
{
//fileData.append(stTemp.nextToken());// + replacerString);
fileData.append(stTemp.nextToken() + replacerString);
}
if(file.exists())
file.delete();
file = new File(filePath);
if(!file.exists())
file.createNewFile();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeChars(fileData.toString());
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..... -
String file_str = ......
-
if(file_str.indexOf(replaceString)!=-1){
-
StringBuffer temp_str = new StringBuffer(file_str);
-
boolean complete = false;
-
int start_pos;
-
while(!complete)
-
if((start_pos=temp_str.toString().indexOf(replaceString))!-=1) temp_str = temp_str.replace(start_pos,replace_str.length(),replacerString);
-
String replaced_str = temp_str.toString();
-
}
-
I think it will work.
Debasis Jana
Don't use DataStreams for text reading/writing purposes; use Readers and Writers
instead. They're made for that job.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: beliavsky |
last post by:
The code
for text in open("file.txt","r"):
print text.replace("foo","bar")
replaces 'foo' with 'bar' in a file, but how do I avoid changing text
inside single or double quotes? For making...
|
by: Epson Barnett |
last post by:
I'm working on learning .NET and I'm curious about the reason for
using static methods in some cases. Specifically, the string class has
a split and a join method, but only the join method is...
|
by: Steve |
last post by:
Hi Guys,
I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of
the string for a given number of...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Matt |
last post by:
string format formula to write a text file
below is not working for me
pos 1 to 10 name
pos 15 to 30 lastname
pos 45 to 75 job
Adam smith programmer
string fmt = "{1,10}{15-30}{45,75}"...
|
by: rsine |
last post by:
I have developed a program that sends a command through the serial port
to our business system and then reads from the buffer looking for a
number. Everything worked great on my WinXP system, but...
|
by: April |
last post by:
Hope someone can help me
The SQL string i need to use does not work, Who can help me....
Thanks in advance
Dim strConn As String
Dim strSQL As String
|
by: Xcriber51 |
last post by:
Hi --
I'm not entirely familiar with the norms and standard libraries of
JavaScript so if the answer to this is yesterday's news, please ignore.
I'm trying to write a simple text formatting...
|
by: coconet |
last post by:
I have a string like this
mystring = "color1:blue;color2:red";
I am tring to convert this into an IDictionary populated like this:
color1 blue
color2 red
My non-working syntax is
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |