我从网上找到了这段代码。但是它好像只适用于4096字节,而且在我看来很丑陋。有没有更好的方法?顺便说一句,我实际上在使用Groovy...
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