【问题标题】:BufferedInputStream To String Conversion? [duplicate]BufferedInputStream 到字符串的转换? [复制]
【发布时间】:2011-08-08 12:04:39
【问题描述】:

可能重复:
In Java how do a read/convert an InputStream in to a string?

您好,我想将此 BufferedInputStream 转换为我的字符串。我该怎么做?

BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream() );
String a= in.read();

【问题讨论】:

    标签: java string bufferedinputstream


    【解决方案1】:
    BufferedInputStream in = new BufferedInputStream(sktClient.getInputStream());
    byte[] contents = new byte[1024];
    
    int bytesRead = 0;
    String strFileContents; 
    while((bytesRead = in.read(contents)) != -1) { 
        strFileContents += new String(contents, 0, bytesRead);              
    }
    
    System.out.print(strFileContents);
    

    【讨论】:

    • 一个小错误。在 while 循环中,您应该在每次迭代中附加。它应该是 += 而不是 =。即:strFileContents += new String(contents, 0, bytesRead);
    • @JJ_Coder4Hire 这不是唯一的错误,此代码依赖于字符串编码在 bytesRead 标记处有边界的可能性(对于 ASCII 来说这是一个好的假设 ONLY)。
    • 我必须输入 'System.out.print(strFileContents);'在循环内,否则只显示我的 html 响应的最后一段。顺便说一句谢谢
    【解决方案2】:

    Guava:

    new String(ByteStreams.toByteArray(inputStream),Charsets.UTF_8);
    

    Commons / IO:

    IOUtils.toString(inputStream, "UTF-8")
    

    【讨论】:

      【解决方案3】:

      我建议你使用 apache commons IOUtils

      String text = IOUtils.toString(sktClient.getInputStream());
      

      【讨论】:

        【解决方案4】:

        请关注代码

        告诉我结果

        public String convertStreamToString(InputStream is)
                        throws IOException {
                    /*
                     * To convert the InputStream to String we use the
                     * Reader.read(char[] buffer) method. We iterate until the
            35.         * Reader return -1 which means there's no more data to
            36.         * read. We use the StringWriter class to produce the string.
            37.         */
                    if (is != null) {
                        Writer writer = new StringWriter();
        
                        char[] buffer = new char[1024];
                        try
                        {
                            Reader reader = new BufferedReader(
                                    new InputStreamReader(is, "UTF-8"));
                            int n;
                            while ((n = reader.read(buffer)) != -1) 
                            {
                                writer.write(buffer, 0, n);
                            }
                        }
                        finally 
                        {
                            is.close();
                        }
                        return writer.toString();
                    } else {       
                        return "";
                    }
                }
        

        谢谢, 刽子手

        【讨论】:

        • 不需要强制转换。 BufferedInputStream 是一个 InputStream
        • “谢谢,Kariyachan” 我记得《来自联合国教科文组织的人》中的那只猫- 他现在是程序员了?
        【解决方案5】:

        如果您不想自己编写所有代码(而且您真的不应该这样做),请使用可以为您完成这些任务的库。

        Apache commons-io 就是这样做的。

        如果您想要更精细的控制,请使用 IOUtils.toString(InputStream) 或 IOUtils.readLines(InputStream)。

        【讨论】:

          猜你喜欢
          • 2014-03-25
          • 1970-01-01
          • 2018-02-26
          • 2013-09-05
          • 1970-01-01
          • 1970-01-01
          • 2020-02-19
          • 2014-06-09
          • 1970-01-01
          相关资源
          最近更新 更多