我有一个方便的函数,已在Java中使用过,用于将InputStream转换为String。这是Scala的直接翻译:
def inputStreamToString(is: InputStream) = {
val rd: BufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))
val builder = new StringBuilder()
try {
var line = rd.readLine
while (line != null) {
builder.append(line + "\n")
line = rd.readLine
}
} finally {
rd.close
}
builder.toString
}
在Scala中有惯用的方法吗?