我应该使用 DataInputStream 还是 BufferedInputStream [英] Should I use DataInputStream or BufferedInputStream

查看:28
本文介绍了我应该使用 DataInputStream 还是 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.

推荐答案

使用包裹在 InputStreamReader 中的普通 InputStream(例如 FileInputStream)然后包装在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆