使用 BufferedReader.readLine() 读取 inputStream 太慢 [英] Reading inputStream using BufferedReader.readLine() is too slow

查看:41
本文介绍了使用 BufferedReader.readLine() 读取 inputStream 太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码.

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;

StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
    responseData.append(line);
}

但是读取 200 行需要超过 12 秒的时间.

But it is taking more than 12 sec to read 200 line.

请帮忙

推荐答案

我强烈怀疑这是因为网络连接或您正在与之交谈的 Web 服务器 - 这不是 BufferedReader 的错.尝试衡量这一点:

I strongly suspect that's because of the network connection or the web server you're talking to - it's not BufferedReader's fault. Try measuring this:

InputStream stream = conn.getInputStream();
byte[] buffer = new byte[1000];
// Start timing
while (stream.read(buffer) > 0)
{
}
// End timing

我想您会发现这与解析文本的时间几乎完全相同.

I think you'll find it's almost exactly the same time as when you're parsing the text.

请注意,您还应该为 InputStreamReader 提供适当的编码 - 平台默认编码几乎肯定不是您应该使用的.

Note that you should also give InputStreamReader an appropriate encoding - the platform default encoding is almost certainly not what you should be using.

这篇关于使用 BufferedReader.readLine() 读取 inputStream 太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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