本文介绍了要写入文本文件的哪个OutputStream子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序会将数据输出到.txt文件,使用诸如NotePad之类的程序的人可以读取该文件.也许不一定是ASCII,而是用户可以理解的内容.

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.

我要使用其中哪一个?

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

我有这个作业,要求我专门使用OutputStream子类之一,因此Writer不是一个选择.

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

课程概述

  • ByteArrayOutputStream 直到BalusC给出了更简单的描述之前,我才理解它.显然,它将内容存储在字节数组中,这使它们不可读,因为它们特别是字节.
  • FileOutputStream 它将输出转换为字节,因此也不可读.
  • FilterOutputStream ,我认为它以我不了解的方式转换数据.当您只想在程序中编写内容时就不好用了.
  • ObjectOutputStream ,根据原始数据类型也可以使用DataOutput中的适当方法写入流中.也可以使用writeUTF方法写入字符串."例子提供了答案,但这又使我感到困惑.
  • OutputStream 完全不了解该描述.
  • PipedOutputStream 它创建二进制数据管道.什么是管道?不是一个线索.
  • ByteArrayOutputStreamdidn'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.

推荐答案

  • ByteArrayOutputStream 是将字节写入内存中的byte[].
  • FileOutputStream 是将字节写入 File
  • FilterOutputStream 是一个通用的超类,用于更特定的输出流,这些输出流可预先处理数据,例如加密/解密,计算校验和,字符编码,压缩(压缩)等.它本身并没有什么特别的.
  • ObjectOutputStream 以序列化的形式将有价值的Java类型和对象写入字节流.基本上,它可以将复杂的Java对象转换为原始字节,反之亦然.
  • OutputStream 是只是这些流的通用抽象类.无论如何,您都无法构建它.但是,您可以对此声明.
  • PipedOutputStream 是旨在能够在管道中写入另一个InputStream,以便另一端可以从该InputStream中读取它们.
    • 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.
    • 您想将数据纯写到 File ,因此 绰绰有余.

      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();
      }
      

      请注意 String#getBytes() 使用平台默认编码将字符转换为字节.如果您使用的是特殊字符",至少不包含在ASCII中的字符,那么您应该始终使用 String#getBytes(charset) .例如:

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


      无关与具体问题正常做法无关,但是,它是使用 Writer 来写入字符数据.


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

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

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

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

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

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

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

      If you care about character encoding, use OutputStreamWriter:

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

      它允许您在使用OutputStream时将字符集指定为第二个参数.

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

      这篇关于要写入文本文件的哪个OutputStream子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 07:45
查看更多