从InputStreamReader(JAVA)读取多行
我有一个 InputStreamReader 对象。我想使用一个函数调用将多行读入缓冲区/数组(无需创建大量字符串对象)。有没有一种简单的方法可以做到这一点?
I have an InputStreamReader object. I want to read multiple lines into a buffer/array using one function call (without crating a mass of string objects). Is there a simple way to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先要注意的是,
InputStreamReader
效率不高,您应该将其包装在BufferedReader
对象周围,以获得最大性能。考虑到这一点,您可以执行以下操作:
请注意,当达到
EOF
时,readLine()
返回null
,因此您应该检查并采取照顾它。First of all mind that
InputStreamReader
is not so efficient, you should wrap it around aBufferedReader
object for maximum performance.Taken into account this you can do something like this:
Mind that
readLine()
returnsnull
isEOF
is reached, so you should check and take care of it.如果您有多行有一些分隔符,您可以使用带有长度和偏移量的 read 方法读取那么多字符。否则,使用 StringBuilder 附加 BufferedReader 读取的每一行应该很适合您,而不会占用太多临时内存
If you have some delimiter for multiple lines you can read that many characters using read method with length and offset. Otherwise using a StringBuilder for appending each line read by BufferedReader should work well for you without eating up too much temp memory