如何使用Java编辑文件中的模板文档(表单字母)?

Chr*_*mbo 3 java templates

我是第一次玩Java,需要能够替换模板中的一些单词.示例模板 -

"亲爱的PUT_THEIR_NAME_HERE,

我正联系你..... bla bla bla

问候,

PUT_COMPANY_NAME_HERE"

什么是最简单的方法(最好使用标准库)制作此模板文件的副本并在正确的位置添加正确的单词然后将其保存到文件系统?我必须做很多这样简单的模板,所以一种易于复制的方式会很好.

我也是使用Rhino通过JavaScript访问Java,不确定这是否有所不同.

问候,

克里斯

Ner*_*est 12

您正在寻找java.text.MessageFormat:

以下是一些使用示例(来自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.
Run Code Online (Sandbox Code Playgroud)


Iva*_*rov 5

对于高级功能(如循环),您可能需要使用Apache VelocityFreeMarker.