最佳答案
我有一个在 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 中有没有这样做的惯用方法?