我应该使用DataInputStream还是BufferedInputStream [英] Should I use DataInputStream or BufferedInputStream
问题描述
我想从文本文件中读取每一行并将它们存储在ArrayList中(每行是ArrayList中的一个条目)。
I want to read each line from a text file and store them in an ArrayList (each line being one entry in the ArrayList).
到目前为止,我理解一个BufferedInputStream写入缓冲区,只有在缓冲区为空时才执行另一次读取,这样可以最小化或至少减少操作系统操作量。
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.
我是否正确 - 我是否有意义吗?
Am I correct - do I make sense?
如果上述情况在任何情况下都会有人想要使用DataInputStream。最后我应该使用哪两个以及为什么 - 或者无关紧要。
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.
推荐答案
使用正常 InputStream
(例如 FileInputStream
)包装在 InputStreamReader
中,然后包装在 BufferedReader
- 然后在 BufferedReader
上调用 readLine
。
Use a normal InputStream
(e.g. FileInputStream
) wrapped in an InputStreamReader
and then wrapped in a BufferedReader
- then call readLine
on the BufferedReader
.
DataInputStream
适用于读取基元,长度为前缀的字符串等。
DataInputStream
is good for reading primitives, length-prefixed strings etc.
这篇关于我应该使用DataInputStream还是BufferedInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!