Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将流转换为字符串Java/Groovy_Java_Groovy - Fatal编程技术网

将流转换为字符串Java/Groovy

将流转换为字符串Java/Groovy,java,groovy,Java,Groovy,我从网上偷了这个片段。但它看起来仅限于4096字节,在我看来相当难看。有谁知道更好的方法吗?我实际上正在使用Groovy btw String streamToString(InputStream input) { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = input.read(b)) != -1;) {

我从网上偷了这个片段。但它看起来仅限于4096字节,在我看来相当难看。有谁知道更好的方法吗?我实际上正在使用Groovy btw

String streamToString(InputStream input) {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = input.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }
编辑:

我在Groovy中找到了一个更好的解决方案:

InputStream exportTemplateStream = getClass().getClassLoader().getResourceAsStream("export.template")
assert exportTemplateStream: "[export.template stream] resource not found"
String exportTemplate = exportTemplateStream.text

使用该类,您可以相当轻松地完成此操作:


它以4096字节(4KB)的数据块读取输入,但实际字符串的大小不受限制,因为它不断读取更多数据并将其附加到SringBuffer中。

请尝试使用Apache Commons:

String s = IOUtils.toString(inputStream, "UTF-8");

该代码段有一个bug:如果输入使用多字节字符编码,则单个字符很有可能跨越两次读取(并且不可转换)。它还有一个半缺陷,即它依赖于平台的默认编码


相反,使用。特别是
IOUtils.toString()
的版本,它接受
InputStream
并对其应用编码。

一些好的快速答案。然而,我认为最好的方法是Groovy在InputStream中添加了一个“getText”方法。所以我所要做的就是
stream.text
。关于4096评论,这是一个很好的呼吁。

对于有类似问题的未来审阅者,请注意,来自Apache的IOUTIL和Groovy的InputStream.getText()方法都要求完成流,或者在返回之前关闭流。如果您使用的是持久流,则需要处理Phil最初发布的“丑陋”示例,或者使用非阻塞IO。

您可以尝试类似的方法

new FileInputStream( new File("c:/tmp/file.txt") ).eachLine { println it }
对于Groovy

filePath = ... //< a FilePath object
stream = filePath.read() //< InputStream object

// Specify the encoding, and get the String object
//content = stream.getText("UTF-16") 
content = stream.getText("UTF-8") 
filePath=…/<文件路径对象
stream=filePath.read()//


getText()
如果不编码,它将使用当前系统编码,例如(“UTF-8”)。

您需要传递一个字符集,否则您将有一个难以诊断的错误。@另外,此代码中可能有许多错误。代码仅作为示例提供;不用于生产。@jinguy-呈现错误代码是否“改善了互联网”?或者干脆添加到rep?@jinguy-如果这会伤害到一个投票率最高的人,我看不出这会对互联网有什么改善。@Anon,我的观点是,人们不应该像写的那样从互联网上复制代码并直接在代码中使用。所以你问了之后检查了文档?至少你可以粘贴一个它的使用示例。我问了之后继续查看。当然,一开始我没有看到.text方法。以下是代码片段:InputStream exportTemplateStream=getClass().getClassLoader().getResourceAsStream(“export.template”)断言exportTemplateStream:“[export.template stream]未找到资源”字符串exportTemplate=exportTemplateStream.textfair。但正如我对其他人说过的:使用带有显式字符集的变体——默认字符集很少是您想要的。
filePath = ... //< a FilePath object
stream = filePath.read() //< InputStream object

// Specify the encoding, and get the String object
//content = stream.getText("UTF-16") 
content = stream.getText("UTF-8")