Which OutputStream subclass to write to a text file

依然范特西╮ 提交于 2020-06-25 23:26:02

问题


I am writing a program that will output data to a .txt file, that can be read by a person using a program like NotePad. Perhaps not necessarily ASCII, but something that the user can understand.

Which one of these do I use?

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

I have this assignment that asks me to use one of OutputStream subclasses specifically, so a Writer is not an option.

Overview of the classes

  • ByteArrayOutputStream didn't understand it until BalusC gave a more simple description. Apparently it stores things in an array of bytes which makes it unreadable since they are specially bytes.
  • FileOutputStream it makes the output into bytes, so it's also unreadable.
  • FilterOutputStream I think it transform the data in ways I don't understand. Not good when you just want to write things as you write them in the program.
  • ObjectOutputStream according to "Primitive data types can also be written to the stream using the appropriate methods from DataOutput. Strings can also be written using the writeUTF method." and the example provided this might be the answer, but again so much explanation again confuses me.
  • OutputStream Don't understand the description at all.
  • PipedOutputStream It creates pipes of binary data. What are pipes? not a clue.

回答1:


  • The ByteArrayOutputStream is to write bytes to an in-memory byte[].
  • 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 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.

OutputStream output = new FileOutputStream("/foo.txt");

try {
    output.write(text.getBytes());
} finally {
    output.close();
}

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("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:

Writer writer = new FileWriter("/foo.txt");

try {
    writer.write(text);
} finally {
    writer.close();
}

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

If you care about character encoding, use OutputStreamWriter:

Writer writer = new OutputStreamWriter(new FileOutputStream("/foo.txt"), "UTF-8");
// ...

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

See also:

  • Java Basic I/O tutorial



回答2:


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");



回答3:


Anything wrong with FileOutputStream?



来源:https://stackoverflow.com/questions/8031737/which-outputstream-subclass-to-write-to-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!