Java 我应该使用 DataInputStream 还是 BufferedInputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/737318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:53:22  来源:igfitidea点击:

Should I use DataInputStream or BufferedInputStream

javaio

提问by Ankur

I want to read each line from a text file and store them in an ArrayList (each line being one entry in the ArrayList).

我想从文本文件中读取每一行并将它们存储在 ArrayList 中(每一行都是 ArrayList 中的一个条目)。

So far I understand that a BufferedInputStream writes to the buffer and only does another read once the buffer is empty which minimises or at least reduces the amount of operating system operations.

到目前为止,我知道 BufferedInputStream 写入缓冲区,并且仅在缓冲区为空时才进行另一次读取,这可以最大限度地减少或至少减少操作系统操作的数量。

Am I correct - do I make sense?

我是对的 - 我有道理吗?

If the above is the case in what situations would anyone want to use DataInputStream. And finally which of the two should I be using and why - or does it not matter.

如果上述情况是在什么情况下有人想要使用 DataInputStream。最后我应该使用这两者中的哪一个以及为什么 - 或者这无关紧要。

采纳答案by Jon Skeet

Use a normal InputStream(e.g. FileInputStream) wrapped in an InputStreamReaderand then wrapped in a BufferedReader- then call readLineon the BufferedReader.

使用正常InputStream(如FileInputStream)包裹在一个InputStreamReader再包上一个BufferedReader-然后调用readLineBufferedReader

DataInputStreamis good for reading primitives, length-prefixed strings etc.

DataInputStream适合读取原语、长度前缀字符串等。

回答by Brian Agnew

I would advocate using Jakarta Commons IOand the readlines()method (of whatever variety).

我提倡使用Jakarta Commons IOreadlines()方法(任何种类)。

It'll look after buffering/closing etc. and give you back a list of text lines. I'll happily roll my own input stream wrapping with buffering etc., but nine times out of ten the Commons IO stuff works fine and is sufficient/more concise/less error prone etc.

它会处理缓冲/关闭等,并给你一个文本行列表。我很乐意使用缓冲等滚动我自己的输入流包装,但十有八九的 Commons IO 东西工作正常,并且足够/更简洁/不易出错等。

回答by Mike

The two classes are not mutually exclusive - you can use both of them if your needs suit.

这两个类并不相互排斥 - 如果需要,您可以同时使用它们。

As you picked up, BufferedInputStream is about reading in blocks of data rather than a single byte at a time. It also provides the convenience method of readLine(). However, it's also used for peeking at data further in the stream then rolling back to a previous part of the stream if required (see the mark() and reset() methods).

正如您所了解的,BufferedInputStream 是关于读取数据块而不是一次读取一个字节。它还提供了方便的 readLine() 方法。但是,它还用于进一步查看流中的数据,然后在需要时回滚到流的前一部分(请参阅 mark() 和 reset() 方法)。

DataInputStream/DataOutputStream provides convenience methods for reading/writing certain data types. For example, it has a method to write/read a UTF String. If you were to do this yourself, you'd have to decide on how to determine the end of the String (i.e. with a terminator byte or by specifying the length of the string).

DataInputStream/DataOutputStream 提供了读取/写入某些数据类型的便捷方法。例如,它有一种写入/读取 UTF 字符串的方法。如果您要自己执行此操作,则必须决定如何确定字符串的结尾(即使用终止符字节或通过指定字符串的长度)。

This is different from BufferedInputStream's readLine() which, as the method sounds like, only returns a single line. writeUTF()/readUTF() deal with Strings - that string can have as many lines it it as it wants.

这与 BufferedInputStream 的 readLine() 不同,正如该方法听起来一样,它只返回一行。writeUTF()/readUTF() 处理字符串——该字符串可以有它想要的任意多行。

BufferedInputStream is suitable for most text processing purposes. If you're doing something special like trying to serialize the fields of a class to a file, you'd want to use DataInput/OutputStream as it offers greater control of the data at a binary level.

BufferedInputStream 适用于大多数文本处理目的。如果您正在做一些特殊的事情,例如尝试将类的字段序列化到文件中,您会想要使用 DataInput/OutputStream,因为它在二进制级别提供了对数据的更好控制。

Hope that helps.

希望有帮助。

回答by Onur

You shoud use DataInputStream in cases when you need to interpret the primitive types in a file written by a language other Java in platform-independent manner.

当您需要以平台无关的方式解释由其他 Java 语言编写的文件中的原始类型时,您应该使用 DataInputStream。

回答by Mike Holler

You can always use both:

您始终可以同时使用两者:

final InputStream inputStream = ...;
final BufferedInputStream bufferedInputStream =
        new BufferedInputStream(inputStream);
final DataInputStream dataInputStream =
        new DataInputStream(bufferedInputStream);

回答by mahir chaudhari

InputStream:Base class to read byte from stream (network or file ), provide ability to read byte from the stream and delete the end of the stream.

InputStream:从流(网络或文件)读取字节的基类,提供从流读取字节和删除流末尾的能力。

DataInputStream:To read data directly as a primitive datatype.

DataInputStream:直接读取数据作为原始数据类型。

BufferInputStream:Read data from the input stream and use buffer to optimize the speed to access the data.

BufferInputStream:从输入流中读取数据,利用buffer优化访问数据的速度。