我有以下文件:
name = David
city = sydney
COuntry = Australia
我正在尝试使用 groovy 创建一个哈希映射=
,并将其拆分并存储在一个数组中,该数组part[0]
包含等于之前和part[1]
之后等于。然后我尝试在此处创建地图。
所需的输出:
def mapedData = [name :david , city : sydney , country :australia ]
我的尝试:
String s=""
def myfile = new File("C:/Users/.............")
BufferedReader br = new BufferedReader(new FileReader(myfile));
Map<String, String> map = new HashMap<String, String>();
while((s = br.readLine()) != null) {
if(!s.startsWith("#")) {
StringTokenizer st=new StringTokenizer(s, "=")
while(st.hasMoreElements()) {
String line=st.nextElement().toString().trim()
print line
}
}
}
}
如果您想从 Groovy 中的文件创建映射,您可以使用java.util.Properties
它。下面是一个例子:
def file = new File("C:\\stackoverflow\\props.properties")
def props = new Properties()
file.withInputStream { stream ->
props.load(stream)
}
println(props)
这打印出来:
[key1:value1, key2:value2]
该props.properties
文件包含以下内容:
# Stackoverflow test
key1 = value1
key2 = value2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句