I would probably create a Map containing all the keys to be replaced ("NAME", "COUNTRY", etc) and all the values that they should be replaced with, loaded from the database. Then create a regular expression to look for a
word to replace:
You might want the
pattern to grab both the word and the text that preceded it:
Then you can use a Scanner to open the file and look for the next occurrance of the pattern. Write a loop to do this repeatedly. Every time you find a new $WORD to replace, write the preceding text to an output file, then look up the word in your map, and write the replacement text to the output file. At the end of the loop, be sure to write all remaining text (that came after the last matched $WORD). Does that help? I don' t know how familiar you are with regular expressions or scanners, so I just gave a quick high-level summary.
Note that you can also use an existing template engine like Velocity or Freemarker.
Here is a nice article about these. The syntax they use may be a little different, but they're probably faster and more efficient than what most programmers will come up with by themselves (without a lot of work on optimizing).