我当前的代码需要从Web上读取外来字符,当前我的解决方案可以运行,但是速度很慢,因为它使用InputStreamReader逐字符读取char。无论如何,有什么可以加快它并完成工作的吗?
// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
StringBuilder contents = new StringBuilder();
int ch;
InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
// FileInputStream file = new InputStream(is);
while( (ch = isr.read()) != -1)
contents.append((char)ch);
String encode = isr.getEncoding();
return contents.toString();
最佳答案
用BufferedReader包裹InputStreamReader
为了有效地阅读
字符,数组和行。
InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
Reader reader = new BufferedReader(isr);
关于java - Java:加快阅读外来字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2610991/