本文介绍了使用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.

请帮助

推荐答案

我强烈怀疑这是因为网络连接或你正在谈论的网络服务器 - 它不是 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太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:06