java:加速读取外文字符
我当前的代码需要从网络读取外来字符,目前我的解决方案可以工作,但速度非常慢,因为它使用 InputStreamReader 逐个字符地读取。有什么办法可以加快速度并完成工作吗?
// 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();
My current code needs to read foreign characters from the web, currently my solution works but it is very slow, since it read char by char using InputStreamReader. Is there anyway to speed it up and also get the job done?
// 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用 BufferedReader 包装您的 InputStreamReader
Wrap your InputStreamReader with a BufferedReader