我在处理this guy 4MB日志文件时遇到了类似的问题。实际上,我正在同时处理多个文件,但是由于我一直得到这个异常,所以我决定只对单个文件进行测试:
val temp = Source.fromFile("./datasource/input.txt")
val dummy = new PrintWriter("test.txt")
var itr = 0
println("Default Buffer size: " + Source.DefaultBufSize)
try {
for( chr <- temp) {
dummy.print(chr.toChar)
itr += 1
if(itr == 75703) println("Passed line 85")
if(itr % 256 == 0){ print("..." + itr); temp.reset; System.gc; }
if(itr == 75703) println("Passed line 87")
if(itr % 2048 == 0) println("")
if(itr == 75703) println("Passed line 89")
}
} finally {
println("\nFalied at itr = " + itr)
}我经常看到的是,在itr = 75703时,它将失败,而输出文件将始终是64 be (65536字节精确)。无论我把temp.reset或System.gc放在哪里,所有的实验都是一样的。
这个问题似乎依赖于一些内存分配,但是我找不到关于这个问题的任何有用的信息。知道怎么解决这个问题吗?
我们非常感谢您的帮助。
编辑:实际上我想把它作为二进制文件处理,所以这种技术不是一个好的解决方案,很多人建议我使用BufferedInputStream。
发布于 2009-10-26 09:32:16
为什么在reset完成对文件的迭代之前在Source上调用它?
val temp = Source.fromFile("./datasource/input.txt")
try {
for (line <- tem p.getLines) {
//whatever
}
finally temp.reset 应该在没有潜流的情况下正常工作。另见this question
https://stackoverflow.com/questions/1623619
复制相似问题