如何使用 groovy 读取文件并将其内容存储为变量?
How to read file using groovy and store its contents are variables?
我正在寻找 groovy 读取文件并将其内容存储为不同变量的特定方法。我的属性文件示例:
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
目前我正在使用这个 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) {
}
}
}
}
我的同事建议使用 groovy 方法来处理这个问题并提到了流,但我似乎找不到太多关于如何将数据存储在单独的变量中的信息,目前我所知道的是def text = new FileInputStream("config.properties").getText("UTF-8")
可以读取整个文件并将其存储在一个变量中,但不能分开。任何帮助将不胜感激
看看 ConfigSlurper:
http://mrhaki.blogspot.de/2009/10/groovy-goodness-using-configslurper.html
如果您愿意让 属性 文件键和 class 属性遵守命名约定,那么您可以很容易地应用 属性 文件值。这是一个例子:
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'
在此示例中,属性 键通过删除“.”进行转换。并将以下字母大写。所以 postgresql.url
变成 postgresqlUrl
。然后只需遍历键并调用 setProperty()
来应用值即可。
我正在寻找 groovy 读取文件并将其内容存储为不同变量的特定方法。我的属性文件示例:
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
目前我正在使用这个 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) {
}
}
}
}
我的同事建议使用 groovy 方法来处理这个问题并提到了流,但我似乎找不到太多关于如何将数据存储在单独的变量中的信息,目前我所知道的是def text = new FileInputStream("config.properties").getText("UTF-8")
可以读取整个文件并将其存储在一个变量中,但不能分开。任何帮助将不胜感激
看看 ConfigSlurper:
http://mrhaki.blogspot.de/2009/10/groovy-goodness-using-configslurper.html
如果您愿意让 属性 文件键和 class 属性遵守命名约定,那么您可以很容易地应用 属性 文件值。这是一个例子:
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'
在此示例中,属性 键通过删除“.”进行转换。并将以下字母大写。所以 postgresql.url
变成 postgresqlUrl
。然后只需遍历键并调用 setProperty()
来应用值即可。