2013-02-11 16 views
2

我正在開發一個scala測試用例,並使用printwriter來填充一些數據。 這沒有任何意義,所以我 - 它看起來像appender只是停止3/4的方式。使用Java printwriter編寫Scala文件 - 爲什麼文件編寫器在此代碼中停止?

你不應該需要的字碼數,你可以看到文件的最後一行是不完整的:

hello world duck duck sauce sauce mazing ninjakeyboard skills ninja 
hello world duck duck sauce 

登錄:

[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) 

代碼:

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) 
    } 
} 
+1

你剛剛添加'testFile.close()'?我發誓失蹤了一分鐘。 – huynhjl 2013-02-11 00:32:08

+0

雅我加了它,它解決了這個問題。愚蠢的錯誤。 Scala io Source庫不需要關閉等,但java庫顯然仍然可以! – JasonG 2013-02-11 15:04:54

回答

1

它看起來像緩衝區沒有沖洗 - 我已經遺漏了file.close(),並添加它後,它的作品! 對不起,浪費你的時間。

謝謝,