我试图通过scala rest call命中tally服务器并将输出保存在文件
中当我卷曲时,我得到了大约412K的输出线 但是当我通过scala命中时,我只获得了411K行(大约缺少500行)
这是因为一些缓冲区大小问题
以下是我的代码
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length))
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8")
httpConn.setRequestMethod("POST")
httpConn.setDoOutput(true)
httpConn.setDoInput(true)
val out = httpConn.getOutputStream
out.write(b)
out.close()
val isr = new InputStreamReader(httpConn.getInputStream)
val in = new BufferedReader(isr)
var temp: String = null
temp= SaveFile(in) //pass stream to save into file
并通过
保存文件 def SaveFile(a: BufferedReader): String = {
val file = new File("OPinWorkspace.xml")
val bw = new BufferedWriter(new FileWriter(file))
Iterator
.continually (a.read)
.takeWhile (-1 !=)
.foreach (bw.write)
return foo
}
答案 0 :(得分:2)
完成后你需要close
你的流。
close
首先刷新流。
try {
// ...
} finally {
a.close
if(bw != null) bw.close // close and flush
}