我应该使用DataInputStream还是BufferedInputStream ?
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.
到目前为止,我理解buffer爱丁堡putstream向缓冲区写入,并且只有在缓冲区为空时才进行另一次读取,这将最小化或至少减少操作系统操作的数量。
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。最后,我应该使用这两种方法中的哪一种,为什么-或者它不重要。
6 个解决方案
#1
16
Use a normal InputStream
(e.g. FileInputStream
) wrapped in an InputStreamReader
and then wrapped in a BufferedReader
- then call readLine
on the BufferedReader
.
使用通常的InputStream(例如FileInputStream)包装在InputStreamReader中,然后包装在BufferedReader中——然后在BufferedReader上调用readLine。
DataInputStream
is good for reading primitives, length-prefixed strings etc.
DataInputStream适用于读取原语、长前缀字符串等。