Today, a gadget was created to generate Latext Tex files through velocity, but when the PDF was generated using MiKTeX, the Chinese became garbled. Before Eclipse ran directly, no problem was found. There is no doubt that the problem is caused by file encoding.
Open the generated Tex file with notepad++, and find that the file's encoding is ANSI, which is the local encoding of the system. Here's the code to generate Tex:
public class Velocityhelper {
private static velocitycontext VC;
static {
VC = new Velocitycontext ();
}
public static void Generatefile (String tempatepath, String destpath, map<string, object> attributes) {
Template Template = velocity.gettemplate (Tempatepath);
For (String Key:attributes.keySet ()) {
vc.put (key, Attributes.get (key));
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter (new FileWriter (DestPath));
Template.merge (VC, BW);
Bw.flush ();
} catch (IOException e) {
e.printstacktrace ();
} finally {
if (bw!= null) {
try {
bw.close ();
The catch (IOException e) {}}}}
Google found two ways to solve the garbled problem:
1. Specify the encoding when obtaining the template file, namely:
Template Template = velocity.gettemplate (Tempatepath, "UTF-8");
2. Specify the encoding when the file is generated, that is:
Template.merge (VC, BW);
Velocity.mergetemplate (DestPath, "UTF-8", VC, BW);
But these two methods do not work. By chance, you see the initial BufferedWriter code, which is the key code to generate the file, and modify it to:
BW = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (DestPath), "UTF-8"));
Finally saw the long absence of Chinese.