scala InputStreamReader不读取整个数据(文件)

时间:2014-07-04 06:14:11

标签: java scala rest http

我试图通过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
}

1 个答案:

答案 0 :(得分:2)

完成后你需要close你的流。

close首先刷新流。

try { 
    // ...
} finally {
    a.close
    if(bw != null) bw.close     // close and flush
}