File groovy:解析具有不同扩展名的文件并使用if/else
我有一个剧本:File groovy:解析具有不同扩展名的文件并使用if/else,file,groovy,File,Groovy,我有一个剧本: def tmn_file = ~/.*\.tmn/ def tmc_file = ~/.*\.tmc/ def newTerm = new Properties().with { props -> new File(inputPath).eachFile(tmn_file) { file -> file.withReader { reader -> load( reader ) pri
def tmn_file = ~/.*\.tmn/
def tmc_file = ~/.*\.tmc/
def newTerm = new Properties().with { props ->
new File(inputPath).eachFile(tmn_file) { file ->
file.withReader { reader ->
load( reader )
println "Read data from file $file:"
something read from file...
switch( props.ACTION ) {
case 'NEW':
do something...
}
switch( props.ACTION ) {
case 'CHANGE':
do something...
}
此脚本在目录中查找,路径为inputPath文件,扩展名为tmn_文件,其中可以包含操作-新建或更改
脚本工作得很好,但我想做另一件事:
如果文件扩展名为*.tmn(tmn_文件)-仅对新案例启动操作
如果文件扩展名为*.tmc(tmc_文件)-仅启动带有更改案例的操作
如何实现决策?以下是解决方案:
这不是switch语句的正确用法。。。您尝试添加了哪些更改?关于Groovy(以及其他所有语言)中switch语句的一点理论:
new Properties().with { props ->
new File(inputPath).eachFile(FileType.FILES) { file ->
file.withReader { reader ->
load(reader)
println "Read data from file $file:"
if (file.name.endsWith('tmn') & props.ACTION == 'NEW' || file.name.endsWith('tmc') & props.ACTION == 'CHANGE') {
// NEW mode
switch( props.ACTION ) {
case 'NEW':
...do someth...
break
// CHANGE mode
case 'CHANGE':
println "***CHANGE mode is on***"
...do someth...
break
default:
throw new RuntimeException("Unknown ACTION $props.ACTION")
}
} else {
if (file.name.endsWith('tmn') || file.name.endsWith('tmc')){
println "$file dont match for action $props.ACTION"
} else {
println "$file have wrong extension "}
}
}
}
}