Let me tell you why.
The problem is that Although you have specified the xml's encoding, that is to say your output will include the following line:
"<?xml version="1.0" encoding="gb2312" ?>", but the problem is that you you File writer is still a wrong encoding stly. So you have to specified your file writer to "gb2312". so try this:
import org.jdom.*;
import org.xml.sax.*;
import org.jdom.output.*;
import java.io.*;
public class TestChinese {
public static void main(String[] args) {
Element root = new Element("root");
Document mydoc = new Document(root);
root.addContent("你好");
try
{
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.setEncoding("gb2312");
outputter.output(mydoc, System.out); FileOutputStream fout = new FileOutputStream("C:\\test.xml");
OutputStreamWriter writer = new OutputStreamWriter(fout, "gb2312");
System.out.println(writer.getEncoding());
outputter.output(mydoc, writer);
writer.close(); }
catch(IOException e)
{
//Eat it!
}
}
}
The problem is that Although you have specified the xml's encoding, that is to say your output will include the following line:
"<?xml version="1.0" encoding="gb2312" ?>", but the problem is that you you File writer is still a wrong encoding stly. So you have to specified your file writer to "gb2312". so try this:
import org.jdom.*;
import org.xml.sax.*;
import org.jdom.output.*;
import java.io.*;
public class TestChinese {
public static void main(String[] args) {
Element root = new Element("root");
Document mydoc = new Document(root);
root.addContent("你好");
try
{
XMLOutputter outputter = new XMLOutputter(" ", true);
outputter.setEncoding("gb2312");
outputter.output(mydoc, System.out); FileOutputStream fout = new FileOutputStream("C:\\test.xml");
OutputStreamWriter writer = new OutputStreamWriter(fout, "gb2312");
System.out.println(writer.getEncoding());
outputter.output(mydoc, writer);
writer.close(); }
catch(IOException e)
{
//Eat it!
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货