tags:

views:

944

answers:

7

I'm playing with Java for the first time and need to be able to replace some words in a template. example template -

"Dear PUT_THEIR_NAME_HERE,

I'm contacting you ..... bla bla bla

Regards,

PUT_COMPANY_NAME_HERE"

What's the simplest way (preferably using the standard library) to make a copy of this template file and add the correct words at the correct place then save it to the file system? I have to do many such simple templates so a way that can be easily replicated would be nice.

I'm also accessing Java through JavaScript using Rhino, not sure if this makes any difference or not.

Regards,

Chris

A: 

I wrote a class for this when I ported an app to java ~10 years ago:

http://github.com/dustin/spyjar/tree/master/src/java/net/spy/util/SpyToker.java

Dustin
A: 

Well, the simplest way would be to read the file into a string, do a replaceAll (once for each word you want to replace) and then write the result out to a new file. This isn't the most efficient approach but it works quite well for simple tasks.

jdigital
+2  A: 

Really simplistic way:

Not bullet proof of course.

C:\oreyes\samples\java\replace>type Simplistic.java
public class Simplistic{
    public static void main( String [] args ) {
        String template = "Dear _NAME_HERE_. I'm glad you...";
        System.out.println( template.replaceAll("_NAME_HERE_","Oscar Reyes"));
    }
}

C:\oreyes\samples\java\replace>java Simplistic
Dear Oscar Reyes. I'm glad you...

Here's the formal doc for that method if you want to investigate further

http://java.sun.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)

I'm glad to hear you're learning java!

OscarRyz
just make sure the template field don't got no regex operators. template.replace might be a valid substitute if she do.
Ellery Newcomer
I should change my name to PUT_COMPANY_NAME_HERE. ;)
Tom Hawtin - tackline
+10  A: 

You're looking for java.text.MessageFormat:

Here are some examples of usage (from JavaDoc):

 Object[] arguments = {
     new Integer(7),
     new Date(System.currentTimeMillis()),
     "a disturbance in the Force"
 };

 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     arguments);

 output: At 12:30 PM on Jul 3, 2053, there was a disturbance
           in the Force on planet 7.
Nerdfest
A: 

You can Use the StringSubstitutor class of commons-lang library

iberck
+4  A: 

For advanced features (like loops) you may want to use Apache Velocity or FreeMarker.

Ivan Dubrov
Velocity is one of the nicest to use templating engines for doing anything beyond a simple string substitution. Turns out it's also the engine that powers the templates inside Intellij IDEa.
Aidos
+1  A: 

The current highest rated post is accurate but not the most terse way to use the API in Java 6. There is no need to create the arguments array or explicitly box the integer and new Date() uses the current time by default:

String result = MessageFormat.format("At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", 7, new Date(), "a disturbance in the force");

Just a few tips to make your code more readable and easier to maintain.

spullara