Find and replace a word in a file using java

R

rynox

Hi Everyone,

My situation is I have a .jsp file which i want to edit a value. Can
java read the file, search for the particular word and then modify that
word into another value ? Any links to read on this or example.

Thanks.
 
R

Roedy Green

My situation is I have a .jsp file which i want to edit a value. Can
java read the file, search for the particular word and then modify that
word into another value ? Any links to read on this or example.

You are trying to modify your source code on the fly?

Read the file as one great string. You can read it in a single
unbuffered i/o. See http://mindprod.com/jgloss/fileio.html
for how.

Now you indexOf to find the word. Now write out the prefix. write the
word, write the suffix.

that saves creating a giant StringBuffer/String
 
R

Rhino

rynox said:
Hi Everyone,

My situation is I have a .jsp file which i want to edit a value. Can
java read the file, search for the particular word and then modify that
word into another value ? Any links to read on this or example.
Sounds like an awful lot of work to me! Wouldn't it make more sense to use
your text editor or IDE to alter your JSP source?

Rhino
 
C

charles_n_may

Can you alter your JSP to read the value on the fly from a properties
file, or some dynamic value from the HttpRequest? Alternatively, how
about changing the jsp during your build process using an Ant <replace>
or <replaceregexp> task?
 
M

Malte

rynox said:
Hi Everyone,

My situation is I have a .jsp file which i want to edit a value. Can
java read the file, search for the particular word and then modify that
word into another value ? Any links to read on this or example.

Thanks.

Combination of awk/sed should get you going.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top