简体   繁体   English

有什么方法可以将PrintWriter转换为ByteArrayOutputStream吗?

[英]Is there any way to convert PrintWriter into ByteArrayOutputStream?

I want to convert PrintWriter object into ByteArrayOutputStream. 我想将PrintWriter对象转换为ByteArrayOutputStream。 and this I am displaying as a PDF. 这是我要显示为PDF。

I don't know how you are using PrintWriter exactly (please post your code), but instead of converting objects you can write lines directly to ByteArrayOutputStream like this: 我不知道您是如何使用PrintWriter的(请发布您的代码),但是您可以像这样直接将行直接写入ByteArrayOutputStream而不是转换对象:

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream);
pw.write("example");
pw.flush();

or (flush after closing PrintWriter ): 或(在关闭PrintWriter之后刷新):

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream);
pw.write("example");
pw.close();

or (auto-flushable): 或(可自动冲洗):

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(byteStream, true);
pw.println("example");

Let me know whether you prefer other solution and add some more details then(your code). 让我知道您是否喜欢其他解决方案,然后添加更多详细信息(您的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM