簡體   English   中英

有什么方法可以將PrintWriter轉換為ByteArrayOutputStream嗎?

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

我想將PrintWriter對象轉換為ByteArrayOutputStream。 這是我要顯示為PDF。

我不知道您是如何使用PrintWriter的(請發布您的代碼),但是您可以像這樣直接將行直接寫入ByteArrayOutputStream而不是轉換對象:

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

或(在關閉PrintWriter之后刷新):

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

或(可自動沖洗):

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

讓我知道您是否喜歡其他解決方案,然后添加更多詳細信息(您的代碼)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM