【发布时间】:2011-12-15 21:03:13
【问题描述】:
我想知道在 Groovy 中是否有一种简单的方法来跟踪文件?我知道如何读取文件,但是如何读取文件然后等待添加更多行、读取它们、等待等等...
我确信这是一个非常愚蠢的解决方案:
def lNum = 0
def num= 0
def numLines = 0
def myFile = new File("foo.txt")
def origNumLines = myFile.eachLine { num++ }
def precIndex = origNumLines
while (true) {
num = 0
lNum = 0
numLines = myFile.eachLine { num++ }
if (numLines > origNumLines) {
myFile.eachLine({ line ->
if (lNum > precIndex) {
println line
}
lNum++
})
}
precIndex = numLines
Thread.sleep(5000)
}
请注意,我对调用 Unix 的“tail”命令并不感兴趣。除非它是唯一的解决方案。
【问题讨论】:
标签: groovy