StringWriter生成的编码格式
时间: 2024-01-16 11:04:16 浏览: 118
编码格式
使用`StringWriter`类生成的字符串编码格式取决于当前Java虚拟机的默认字符编码,通常是UTF-8或者UTF-16。如果需要指定生成的字符串编码格式,可以使用`OutputStreamWriter`类将`StringWriter`输出到`ByteArrayOutputStream`中,然后通过指定编码格式生成字符串,例如:
```java
StringWriter stringWriter = new StringWriter();
marshaller.marshal(obj, stringWriter);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(stringWriter.toString());
writer.flush();
String xmlStr = new String(outputStream.toByteArray(), "UTF-8");
```
这样可以将生成的XML字符串编码格式指定为UTF-8。
阅读全文