问题描述
我有一个输入文件,一个Groovy文件从中读取输入.处理完特定的输入后,Groovy脚本应该能够注释其使用的输入行,然后继续.
I have a input file from which a Groovy file reads input. Once a particular input is processed, Groovy script should be able to comment the input line it used and then move on.
文件内容:
1
2
3
处理第1行和第2行时,输入文件如下所示:
When it processes line 1 and line 2, the input file will look as below:
'1
'2
3
这样,如果我重新运行Groovy,我想从上次停止的那一行开始.如果使用了输入但输入失败,则不应注释该特定行('
),以便可以尝试重试.
By this way, if I re-run the Groovy, I would like to start from the line it stopped last time. If a input was used and it failed, that particular line shall not be commented ('
) so that a retry can be attempted.
感谢您是否可以帮助起草Groovy脚本.
Appreciate if you can help to draft a Groovy script.
谢谢
推荐答案
AFAIK,您只能在文件末尾附加文本.
AFAIK in Groovy you can only append text at the end of the file.
因此,在处理完每一行后,需要在每行上添加'
,您需要重写整个文件.
Hence to add '
on each line when it is processed you need to rewrite the entire file.
您可以使用以下方法,但是由于您要加载内存中的所有行,因此我建议您仅将其用于较小的文件.总之,您的问题的解决方法可能是:
You can use the follow approach but I only recommend you to use for a small files since you're loading all the lines in memory. In summary an approach for your question could be:
// open the file
def file = new File('/path/to/sample.txt')
// get all lines
def lines = file.readLines()
try{
// for each line
lines.eachWithIndex { line,index ->
// if line not starts with your comment "'"
if(!line.startsWith("'")){
// call your process and make your logic...
// but if it fails you've to throw an exception since
// you can not use 'break' within a closure
if(!yourProcess(line)) throw new Exception()
// line is processed so add the "'"
// to the current line
lines.set(index,"'${line}")
}
}
}catch(Exception e){
// you've to catch the exception in order
// to save the progress in the file
}
// join the lines and rewrite the file
file.text = lines.join(System.properties.'line.separator')
// define your process...
def yourProcess(line){
// I make a simple condition only to test...
return line.size() != 3
}
避免对大型文件加载内存中所有行的最佳方法是使用读取器读取文件内容,并使用带有写入器的临时文件写入结果,而优化版本可能是:
An optimal approach to avoid load all lines in memory for a large files is to use a reader to read the file contents, and a temporary file with a writer to write the result, and optimized version could be:
// open the file
def file = new File('/path/to/sample.txt')
// create the "processed" file
def resultFile = new File('/path/to/sampleProcessed.txt')
try{
// use a writer to write a result
resultFile.withWriter { writer ->
// read the file using a reader
file.withReader{ reader ->
while (line = reader.readLine()) {
// if line not starts with your comment "'"
if(!line.startsWith("'")){
// call your process and make your logic...
// but if it fails you've to throw an exception since
// you can not use 'break' within a closure
if(!yourProcess(line)) throw new Exception()
// line is processed so add the "'"
// to the current line, and writeit in the result file
writer << "'${line}" << System.properties.'line.separator'
}
}
}
}
}catch(Exception e){
// you've to catch the exception in order
// to save the progress in the file
}
// define your process...
def yourProcess(line){
// I make a simple condition only to test...
return line.size() != 3
}
这篇关于Groovy脚本逐行修改文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!