Java:打印流到字符串?

发布于 2024-08-11 22:57:05 字数 108 浏览 8 评论 0原文

我有一个函数,它接受某种类型的对象和要打印的 PrintStream,并输出该对象的表示形式。如何在字符串中捕获该函数的输出?具体来说,我想在 toString 方法中使用它。

I have a function that takes an object of a certain type, and a PrintStream to which to print, and outputs a representation of that object. How can I capture this function's output in a String? Specifically, I want to use it as in a toString method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

待天淡蓝洁白时 2024-08-18 22:57:05

使用 ByteArrayOutputStream 作为缓冲区:

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String utf8 = StandardCharsets.UTF_8.name();
    try (PrintStream ps = new PrintStream(baos, true, utf8)) {
        yourFunction(object, ps);
    }
    String data = baos.toString(utf8);

Use a ByteArrayOutputStream as a buffer:

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String utf8 = StandardCharsets.UTF_8.name();
    try (PrintStream ps = new PrintStream(baos, true, utf8)) {
        yourFunction(object, ps);
    }
    String data = baos.toString(utf8);
筱武穆 2024-08-18 22:57:05

您可以使用传递到构造函数的 ByteArrayOutputStream 构造一个 PrintStream,稍后您可以使用它来获取写入 PrintStream 的文本。

ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
...
String output = os.toString("UTF8");

You can construct a PrintStream with a ByteArrayOutputStream passed into the constructor which you can later use to grab the text written to the PrintStream.

ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(os);
...
String output = os.toString("UTF8");
锦爱 2024-08-18 22:57:05

为什么不将 StringWriter 与 PrintWriter 一起使用?

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("Hello World!");
String output = writer.toString();

Why don't you use a StringWriter with a PrintWriter?

StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
out.println("Hello World!");
String output = writer.toString();
败给现实 2024-08-18 22:57:05

该答案是之前答案的统一,适用于 Java 1.7 及更高版本。另外,我添加了代码来关闭流。

final Charset charset = StandardCharsets.UTF_8;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true, charset.name());
yourFunction(object, ps);
String content = new String(baos.toByteArray(), charset);
ps.close();
baos.close();

A unification of previous answers, this answer works with Java 1.7 and after. Also, I added code to close the Streams.

final Charset charset = StandardCharsets.UTF_8;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true, charset.name());
yourFunction(object, ps);
String content = new String(baos.toByteArray(), charset);
ps.close();
baos.close();
×纯※雪 2024-08-18 22:57:05

也许这个问题可能会对您有所帮助:
将OutputStream放入字符串

Maybe this question might help you:
Get an OutputStream into a String

Subclass OutputStream and wrap it in PrintStream

白首有我共你 2024-08-18 22:57:05

定义并初始化名为 inSS 的 Scanner 变量,该变量使用字符串变量 myStrLine 创建输入字符串流。

答:
扫描仪 inSS = new Scanner(myStrLine);

Define and initialize a Scanner variable named inSS that creates an input string stream using the String variable myStrLine.

Ans:
Scanner inSS = new Scanner(myStrLine);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文