Readline 太慢 - 有更快的吗?
我正在使用 BufferedReader 和 InputStreamReader 从流中读取内容,以创建从读取器创建的一个长字符串。它的行数超过 100,000 行,然后抛出 500 错误(服务器上的调用失败)。我不确定问题是什么,有没有比这种方法更快的方法?当线路数达到数千时它可以工作,但我正在处理大型数据集。
BufferedReader in = new BufferedReader(new InputStreamReader(newConnect.getInputStream()));
String inputLine;
String xmlObject = "";
StringBuffer str = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
str.append(inputLine);
str.toString();
}
in.close();
提前致谢
I am reading in from a stream using a BufferedReader and InputStreamReader to create one long string that gets created from the readers. It gets up to over 100,000 lines and then throws a 500 error (call failed on the server). I am not sure what is the problem, is there anything faster than this method? It works when the lines are in the thousands but i am working with large data sets.
BufferedReader in = new BufferedReader(new InputStreamReader(newConnect.getInputStream()));
String inputLine;
String xmlObject = "";
StringBuffer str = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
str.append(inputLine);
str.toString();
}
in.close();
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有机会这样做来创建您的“长字符串”?
如果是,则将其更改为
String 对象是不可变的,并且当您执行 str+="something" 时,会重新分配内存,并将 str+"something" 复制到新分配的区域。这是一项成本高昂的操作,运行 51,000 次是一件极其糟糕的事情。
StringBuffer和StringBuilder是String的可变兄弟,StringBuilder是非并发的,比StringBuffer效率更高。
Are you by any chance doing this to create your "long string"?
If yes, then change it to
String objects are immutable and when you do str+="something", memory is reallocated and str+"something" is copied to that newly allocated area. This is a costly operation and running it 51,000 times is an extremely bad thing to do.
StringBuffer and StringBuilder are String's mutable brothers and StringBuilder, being non-concurrent is more efficient than StringBuffer.
readline() 的读取速度约为 90 MB/s,这是您对数据读取的操作,速度很慢。 BTW readline 删除了换行符,因此您使用的这种方法是有缺陷的,因为它会将每一行变成一行。
我建议您不要重新发明轮子 FileUtils.readLineToString()
这将有效地将文件读取为字符串而不丢弃换行符。
readline() can read at about 90 MB/s, its what you are doing with the data read which is slow. BTW readline removes newlines so this approach you are using is flawed as it will turn everying into one line.
Rather than re-inventing the wheel I would suggest you try FileUtils.readLineToString()
This will read a file as a STring without discarding newlines, efficiently.