Forum Moderators: phranque

Message Too Old, No Replies

Java Program Problem

Search/Replace using Java

         

neh2008

1:14 pm on Feb 2, 2003 (gmt 0)

10+ Year Member



Hi Folks!

I have an issue which is a little complecated for me as of now.

I want to do a Search/Replace on files which are 2 levels deep from my PWD. I can browse thru the files and view the contents. But, how do I get a word from the file and replace it with another.

My objective is to read a line and "Replace"that line with another one, dynamically generated by the program.

any inputs?

Thank You,
Nehal

ppg

11:19 am on Feb 3, 2003 (gmt 0)

10+ Year Member



Hi neh2008,

You need to have a look at the methods for the String class

[java.sun.com ]

In particular look at the indexOf(String str) method. You can read the file line by line, the above method will give you an integer value for where the string you want to replace starts, like this (myLine being the line from the file, searchString being the String you want to replace):

int position;
position = myLine.indexOf("searchString");

From there its a pretty simple job to replace the string with your new one. Look at the StringBuffer class too for handling your replacement in.

Bear in mind also that its not necessary to re-invent the wheel if you don't want to:
[google.com ]

hope this helps