The Chinese problem of velocity

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.