[英]How to read file using groovy and store its contents are variables?
I'm looking for groovy specific way to read file and store its content as different variables. 我正在寻找读取文件并将其内容存储为不同变量的常规方法。 Example of my properties file: 我的属性文件示例:
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
At the moment I'm using this java code to read the file and use variables: 目前,我正在使用以下Java代码读取文件并使用变量:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
prop.load(input);
this.postgresqlUser = prop.getProperty("postgresql.username")
this.postgresqlPass = prop.getProperty("postgresql.password")
this.postgresqlUrl = prop.getProperty("postgresql.url")
this.consoleUrl = prop.getProperty("console.url")
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
}
}
}
}
My colleague recommended to use groovy way to deal with this and mentioned streams but I can't seem to find much information about on how to store data in separate variables, what I know so far is that def text = new FileInputStream("config.properties").getText("UTF-8")
could read whole file and store it in one variable, but not separate. 我的同事建议使用常规方法来处理此问题,并提到了流,但是我似乎找不到太多有关如何将数据存储在单独变量中的信息,到目前为止,我所知道的是def text = new FileInputStream("config.properties").getText("UTF-8")
可以读取整个文件并将其存储在一个变量中,但不能分开。 Any help would be appreciated 任何帮助,将不胜感激
If you're willing to make your property file keys and class properties abide by a naming convention, then you can apply the property file values quite easily. 如果您愿意使您的属性文件键和类属性遵守命名约定,那么可以很容易地应用属性文件值。 Here's an example: 这是一个例子:
def config = '''
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
'''
def props = new Properties().with {
load(new StringBufferInputStream(config))
delegate
}
class Foo {
def postgresqlUsername
def postgresqlPassword
def postgresqlUrl
def consoleUrl
Foo(Properties props) {
props.each { key, value ->
def propertyName = key.replaceAll(/\../) { it[1].toUpperCase() }
setProperty(propertyName, value)
}
}
}
def a = new Foo(props)
assert a.postgresqlUsername == 'xxxxxxx'
assert a.postgresqlPassword == 'xxxxxxx'
assert a.postgresqlUrl == 'xxxx.xxxx.xxxx'
assert a.consoleUrl == 'xxxxx.xxxx.xxx'
In this example, the property keys are converted by dropping the '.' 在此示例中,通过删除“。”来转换属性键。 and capitalizing the following letter. 并大写以下字母。 So postgresql.url
becomes postgresqlUrl
. 因此postgresql.url
成为postgresqlUrl
。 Then it's just a matter for iterating through the keys and calling setProperty()
to apply the value. 然后,只需遍历键并调用setProperty()
以应用值即可。
Take a look at the ConfigSlurper: 看一下ConfigSlurper:
http://mrhaki.blogspot.de/2009/10/groovy-goodness-using-configslurper.html http://mrhaki.blogspot.de/2009/10/groovy-goodness-using-configslurper.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.