哪个 OutputStream 子类写入文本文件

发布于 2024-12-14 04:03:24 字数 433 浏览 2 评论 0原文

[作业]

要求

专门使用OutputStream子类将数据输出到.txt文件,该文件可以由使用记事本等程序的人读取。 (因此 Writer 不是一个选项)

Thoughts

可以是 ASCII 或任何人类可读的字符集。

问题

我应该使用其中哪一个?

  • ByteArrayOutputStream
  • FileOutputStream
  • FilterOutputStream
  • ObjectOutputStream
  • OutputStream
  • PipedOutputStream

[Assignment]

Requirement

Use specifically OutputStream subclasses to output data to a .txt file, that can be read by a person using a program like Notepad.
(so a Writer is not an option)

Thoughts

May be ASCII or any human-readable character set.

Question

Which one of these do I use?

  • ByteArrayOutputStream
  • FileOutputStream
  • FilterOutputStream
  • ObjectOutputStream
  • OutputStream
  • PipedOutputStream

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

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

发布评论

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

评论(3

携余温的黄昏 2024-12-21 04:03:24
  • ByteArrayOutputStream 是将字节写入内存中的byte[] 变量。
  • FileOutputStream 是将字节写入 文件
  • FilterOutputStream 是更具体的输出流的公共超类,它预先操作数据,例如加密/解密、计算校验和、字符编码、压缩(zipping)等。它本身并没有什么特别的作用。
  • ObjectOutputStream是将完整的Java类型和对象以序列化的形式写入字节流。它基本上允许将复杂的 Java 对象转换为原始字节,反之亦然。
  • OutputStream 只是这些流的公共抽象类。无论如何你都无法构建它。不过,您可以声明反对。
  • PipedOutputStream 旨在能够写入管道中的另一个 InputStream,以便另一端可以从该 InputStream 读取它们。

您希望将数据写入 文件,因此FileOutputStream 就足够了。

try (OutputStream output = new FileOutputStream("/foo.txt")) {
    output.write(text.getBytes());
}

请注意 String#getBytes() 使用平台默认编码将字符转换为字节。如果您使用的是至少 ASCII 未涵盖的“特殊字符”,那么您应该始终使用 String#getBytes(charset)。例如:

    output.write(text.getBytes(StandardCharsets.UTF_8));

与具体问题无关正常做法是使用 Writer 写入字符数据。

如果您不关心字符编码,请使用 FileWriter

try (Writer writer = new FileWriter("/foo.txt")) {
    writer.write(text);
}

它将使用平台默认字符编码,通常也支持 ASCII 字符。

如果您关心字符编码,请使用 OutputStreamWriter

try (Writer writer = new OutputStreamWriter(new FileOutputStream("/foo.txt"), StandardCharsets.UTF_8)) {
    // ...
}

它允许您在获取 OutputStream 时将字符集指定为第二个参数。

另请参阅:

  • The ByteArrayOutputStream is to write bytes to an in-memory byte[] variable.
  • The FileOutputStream is to write bytes to a File.
  • The FilterOutputStream is a common superclass for more specific output streams which manipulate the data beforehand, such as encryption/decryption, calculating checksum, character encoding, compressing (zipping), etcetera. It does by itself nothing special.
  • The ObjectOutputStream is to write fullworthy Java types and objects in a serialized form into a byte stream. It basically allows to convert complex Java objects to raw bytes and vice versa.
  • The OutputStream is just the common abstract class of those streams. You can't construct it anyway. You can however declare against it.
  • The PipedOutputStream is intented to be able to write to another InputStream in a pipe so that the other side can read them from that InputStream.

You want to write the data plain to a File, so the FileOutputStream is more than sufficient.

try (OutputStream output = new FileOutputStream("/foo.txt")) {
    output.write(text.getBytes());
}

Note that String#getBytes() uses the platform default encoding to convert characters to bytes. If you're using "special characters" which are not covered by at least ASCII, then you should always explicitly specify the charset using String#getBytes(charset). E.g.:

    output.write(text.getBytes(StandardCharsets.UTF_8));

Unrelated to the concrete question, the normal practice, however, is to use a Writer to write character data.

If you don't care about character encoding, use FileWriter:

try (Writer writer = new FileWriter("/foo.txt")) {
    writer.write(text);
}

It will use the platform default character encoding which will usually also support ASCII characters.

If you care about character encoding, use OutputStreamWriter:

try (Writer writer = new OutputStreamWriter(new FileOutputStream("/foo.txt"), StandardCharsets.UTF_8)) {
    // ...
}

It allows you for specifying the charset as 2nd argument while taking an OutputStream.

See also:

深海夜未眠 2024-12-21 04:03:24

使用 PrintStream。它是 OutputStream 的子类,允许逐行输出。它与 System.out 中使用的类相同

new PrintStream(new File("path/to/your/file.txt")).println("Your output");

Use a PrintStream. It is a subclass of OutputStream and allows for line-by-line output. It is the same class used in System.out

new PrintStream(new File("path/to/your/file.txt")).println("Your output");
夏见 2024-12-21 04:03:24

FileOutputStream 有什么问题吗?

Anything wrong with FileOutputStream?

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