【发布时间】:2018-09-01 07:58:19
【问题描述】:
我有一个配置文件“Replace_IP_List.cfg”,如下所示
10.19.120.39=[fec0:1111:2222:3333:4444]
[fec0:2222:2222:3333:4444] = 10.18.215.151
我正在读取 cfg 文件以获取键和值,因为键有 ':' ,我无法获得完整的字符串,
Properties prop = new Properties();
InputStream is = new FileInputStream("Replace_IP_List.cfg");
prop.load(is);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements())
{
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
str=str.replace(key, value);
输出:
Key : 10.19.120.39, Value : [fec0:1111:2222:3333:4444]
Key : [fec0, Value : 2222:2222:3333:4444] = 10.18.215.151
cfg 文件是运行时生成的(或由其他人提供)我不需要编辑 cfg 文件或转义为:
任何人都可以建议我在程序内部如何逃脱:并且在阅读文件时只考虑=。预期输出如下,
Key : 10.19.120.39, Value : [fec0:1111:2222:3333:4444]
Key : [fec0:2222:2222:3333:4444], Value : 10.18.215.151
【问题讨论】:
-
是什么阻止你只用
=分割每一行?=符号似乎是唯一的分隔符。 -
我的意思是当
prop.load被调用时,它需要默认分隔符:或=,任何选项仅将=设置为分隔符?
标签: java properties