首页 > 解决方案 > 没有创建 Tmp 文件中的数据

问题描述

我有以下最大规模,我需要在其中创建一个临时文件并添加一些内容,调用 main 方法,然后查看是否产生了正确的输出。

我可以看到在“/tmp”中创建的文件,但它始终是空的。就像 BufferedWriter 没有写入它一样。我究竟做错了什么?

  "provided with a non empty file" should {
    import java.io.{BufferedWriter, File, FileWriter}
    val log: File = File.createTempFile("data", ".log")
    log.renameTo(new File(log.getAbsolutePath + "data.log"))
    val bw = new BufferedWriter(new FileWriter(log))
    bw.write("This is the temporary file content")
    bw.close()
    log.deleteOnExit()
    "print info" in {
      val stream = new java.io.ByteArrayOutputStream()
      Console.withOut(stream) {
        val args = Array(log.getAbsolutePath)
        main(args)
      }
      assert(stream.toString.trim == "This is the temporary file content")
    }
  }
}

标签: scalajava-io

解决方案


推荐阅读