• Post Reply Bookmark Topic Watch Topic
  • New Topic

Efficient text replacement  RSS feed

 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to replace a set of tokens in a text file.

What is a good efficient way to do it? I prefer not to run replaceAll() multiple times. Hopefully it can be streamed into another file directly.

E.g.
"{PERSON} favorite fruit is {FAV_FRUIT}, and {PERSON} hates {HATE_FRUIT}"

Replace tokens with:
PERSON -> "John"
FAV_FRUIT -> "Apple"
HATE_FRUIT -> "Orange"

Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your text format looks suspiciously like something a MessageFormatter might handle. Why not just use one of them?
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is for general replacement, especially for properties files and java source files.

I'm on JDK 1.4.2. I'll start by looking at java.text.MessageFormat to see if it is suitable. Thanks!
[ March 29, 2007: Message edited by: Chu Tan ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!