java:加快阅读外来人物

时间:2010-04-09 21:16:26

标签: java inputstream

我当前的代码需要从网上读取外来字符,目前我的解决方案有效,但速度非常慢,因为它使用InputStreamReader通过char读取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();

1 个答案:

答案 0 :(得分:6)

使用BufferedReader

包装InputStreamReader
  

有效阅读   字符,数组和行。

InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
Reader reader = new BufferedReader(isr);