java:加速读取外文字符

发布于 2024-08-29 01:45:37 字数 632 浏览 5 评论 0原文

我当前的代码需要从网络读取外来字符,目前我的解决方案可以工作,但速度非常慢,因为它使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-09-05 01:45:37

BufferedReader 包装您的 InputStreamReader

为了高效阅读
字符、数组和行。

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

Wrap your InputStreamReader with a BufferedReader

for the efficient reading of
characters, arrays, and lines.

InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
Reader reader = new BufferedReader(isr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文