首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java printwriter编写Scala文件-为什么文件写入器在此代码中停止?

使用Java printwriter编写Scala文件-为什么文件写入器在此代码中停止?
EN

Stack Overflow用户
提问于 2013-02-11 08:27:54
回答 1查看 1.7K关注 0票数 2

我正在编写一个scala测试用例,并使用printwriter填充一些数据。这没有任何意义,所以我-它看起来像附加器只是停止了四分之三的方式。

您不需要用于字数统计的代码,因为您可以看到文件中的最后一行是不完整的:

代码语言:javascript
复制
hello world duck duck sauce sauce mazing ninjakeyboard skills ninja
hello world duck duck sauce

日志:

代码语言:javascript
复制
[info] Give a file with 10 words repeated on 1000 lines and file handler 
[info] - should give us an array of 10000 words *** FAILED ***
[info]   8434 did not equal 10000 (WordCountFileHandler.scala:20)

代码:

代码语言:javascript
复制
import java.io.PrintWriter
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import WordCountFileHandler.WordCountFileHandler

class WordCountFileHandler extends FlatSpec with ShouldMatchers {

  "Give a file with 10 words repeated on 1000 lines and file handler" should "give us an array of 10000 words" in {
    val filename = java.util.UUID.randomUUID().toString
    val testFile = new PrintWriter( filename , "UTF-8")
    for (x <- 1 to 1000) yield {testFile.println("hello world duck duck sauce sauce mazing ninjakeyboard skills ninja")}
    testFile.close()


    val testOutput = WordCountFileHandler (filename)
    testOutput.size should equal(1)
    //testOutput.head.foreach(println(_))
    testOutput.head.size should equal (10000)
  }
}
EN

回答 1

Stack Overflow用户

发布于 2013-02-11 08:34:02

看起来缓冲区没有刷新--我遗漏了file.close(),在添加它之后,它就可以工作了!很抱歉浪费你的时间。

谢谢,

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14804295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档