用groovy解析一个属性文件 [英] Parse a properties file with groovy

查看:186
本文介绍了用groovy解析一个属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从包含以下内容的属性文件中提取用户名和密码:

  #Fri May 31 09:33 :22 CEST 2013 
password = user40_31-05-2013
username = user40_31-05-2013


文件propertiesFile = new File('testdata.properties')
def config = new ConfigSlurper()。parse(propertiesFile.toURL())
println(config.username)

我遇到这个错误:


期待'!',找到'F'@
第1行第2列。#Fri May 31 09:33:22 CEST 2013
^

<1>错误

提前致谢

解决方案

您可以保存关闭流你自己更习惯:

  def props = new Properties()
新文件(foo.properties) .withInputStream {s - >
props.load(s)
}


I'm trying to extract the username and password from a properties file containing :

#Fri May 31 09:33:22 CEST 2013 
password=user40_31-05-2013 
username=user40_31-05-2013


File propertiesFile = new File('testdata.properties')
def config = new ConfigSlurper().parse(propertiesFile.toURL())
println(config.username)

I'm having this error:

expecting '!', found 'F' @ line 1, column 2. #Fri May 31 09:33:22 CEST 2013 ^

1 error

thanks in advance

解决方案

You can save having to close the stream yourself with the more idiomatic:

def props = new Properties()
new File("foo.properties").withInputStream { s ->
  props.load(s) 
}

这篇关于用groovy解析一个属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆