哪个 OutputStream 子类写入文本文件
[作业]
要求
专门使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ByteArrayOutputStream
是将字节写入内存中的byte[]
变量。FileOutputStream
是将字节写入文件
。FilterOutputStream
是更具体的输出流的公共超类,它预先操作数据,例如加密/解密、计算校验和、字符编码、压缩(zipping)等。它本身并没有什么特别的作用。ObjectOutputStream
是将完整的Java类型和对象以序列化的形式写入字节流。它基本上允许将复杂的 Java 对象转换为原始字节,反之亦然。OutputStream
只是这些流的公共抽象类。无论如何你都无法构建它。不过,您可以声明反对。PipedOutputStream
旨在能够写入管道中的另一个InputStream
,以便另一端可以从该InputStream
读取它们。您希望将数据写入
文件
,因此FileOutputStream
就足够了。请注意
String#getBytes()
使用平台默认编码将字符转换为字节。如果您使用的是至少 ASCII 未涵盖的“特殊字符”,那么您应该始终使用String#getBytes(charset)
。例如:与具体问题无关,正常做法是使用
Writer
写入字符数据。如果您不关心字符编码,请使用
FileWriter
:它将使用平台默认字符编码,通常也支持 ASCII 字符。
如果您关心字符编码,请使用
OutputStreamWriter
:它允许您在获取
OutputStream
时将字符集指定为第二个参数。另请参阅:
ByteArrayOutputStream
is to write bytes to an in-memorybyte[]
variable.FileOutputStream
is to write bytes to aFile
.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.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.OutputStream
is just the common abstract class of those streams. You can't construct it anyway. You can however declare against it.PipedOutputStream
is intented to be able to write to anotherInputStream
in a pipe so that the other side can read them from thatInputStream
.You want to write the data plain to a
File
, so theFileOutputStream
is more than sufficient.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 usingString#getBytes(charset)
. E.g.: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
:It will use the platform default character encoding which will usually also support ASCII characters.
If you care about character encoding, use
OutputStreamWriter
:It allows you for specifying the charset as 2nd argument while taking an
OutputStream
.See also:
使用 PrintStream。它是 OutputStream 的子类,允许逐行输出。它与 System.out 中使用的类相同
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
FileOutputStream 有什么问题吗?
Anything wrong with FileOutputStream?