带有lambda的java 8字符串列表函数;我不能在groovy中工作
在java 8中,此代码运行良好:带有lambda的java 8字符串列表函数;我不能在groovy中工作,groovy,lambda,java-8,incompatibility,Groovy,Lambda,Java 8,Incompatibility,在java 8中,此代码运行良好: Stream<String> lines = Files.lines(outfile) { List<String> replaced = lines .map(line -> line.replaceAll('date1', "$newdate1")) .collect(Collectors.toList()) Files.write(o
Stream<String> lines = Files.lines(outfile) {
List<String> replaced = lines
.map(line ->
line.replaceAll('date1', "$newdate1"))
.collect(Collectors.toList())
Files.write(outfile, replaced)
}
streamlines=Files.lines(outfile){
列表替换=行
.map(行->
行.replaceAll('date1',“$newdate1”))
.collect(收集器.toList())
文件。写入(输出文件,替换)
}
在groovy中,因为我使用的是2.6之前的groovy版本,所以它没有。我不能改变它,是在卡塔隆使用的。我收到一个错误“意外令牌->”
我尝试用这两种方法将lambda函数括起来,但都不起作用:
Stream<String> lines = Files.lines(outfile) {
List<String> replaced = lines
.map({line ->
line}.replaceAll('date1', "$newdate1"))
.collect(Collectors.toList())
Files.write(outfile, replaced)
}
streamlines=Files.lines(outfile){
列表替换=行
.map({line->
行}.replaceAll('date1',“$newdate1”))
.collect(收集器.toList())
文件。写入(输出文件,替换)
}
及
streamlines=Files.lines(outfile){
列表替换=行
.map({line->
line.replaceAll('date1',“$newdate1”)})
.collect(收集器.toList())
文件。写入(输出文件,替换)
}
这种包装类型的隔离/取出上下文行,该行未被识别为字符串,replaceAll
不起作用
我不知道什么才是正确的方法,让它工作。Java和Groovy中有额外的花括号,这就是它不工作的原因
List<String> lines = Files.lines(outputFile)
.map({ line -> line.replaceAll('date1', "$newdate1") })
.collect(Collectors.toList())
Files.write(outputFile, lines)
List line=Files.line(outputFile)
.map({line->line.replaceAll('date1',“$newdate1”)})
.collect(收集器.toList())
文件。写入(输出文件,行)
我终于找到了一个非常简单的解决方案(纯groovy):
“将输入文件转换为文本”
fileText=infle.text
'将值date1替换为newdate1'
fileText=fileText.replaceAll('date1',“$newdate1”)
'然后将文本发送到输出文件'
第一种方法既不是有效的Java,也不是有效的Groovy。我不明白第二种变体有什么问题。如果调用了replaceAll
怎么可能不工作?你确定你的参数是正确的吗?这可能会有所帮助-它有一个如何在Java中使用Groovy闭包的示例。Groovy版本:Files.lines(outputFile)*.replaceAll(…)
谢谢你的回答
List<String> lines = Files.lines(outputFile)
.map({ line -> line.replaceAll('date1', "$newdate1") })
.collect(Collectors.toList())
Files.write(outputFile, lines)
'convert input file into text'
fileText = infile.text
'Replace the value date1 with newdate1'
fileText = fileText.replaceAll('date1', "$newdate1")
'then send the text to the output file'
outfile << fileText