OutputStreamWriter java无法存入字符串

时间:2013-06-23 21:49:35

标签: java utf-8 filestreamresult

transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

我正在使用它来生成控制台的输出。我想将输出存储到刺痛中。我不知道该怎么做。谁能帮我? :(

提前致谢

3 个答案:

答案 0 :(得分:7)

例如使用它(它避免编码字符串):

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String str = writer.getBuffer().toString();

答案 1 :(得分:2)

使用StringWriter课程代替OutputStreamWriter

答案 2 :(得分:0)

一个解决方案可能如下所示:

ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(out, "UTF-8")));
String string = new String(out.toByteArray(), "UTF-8");