你可以java.util.Properties
用来读取数据。毕竟,他们只是一key=value
对。
例如:
String input="[123]=\"some string\" [234]=999999999 [345]=\"some other string\"";
input = input.replaceAll("\\s+\\[", System.getProperty("line.separator") + "[");
Reader in = new StringReader(input);
Properties props = new Properties();
props.load(in);
in.close();
要提取数据,请使用以下命令:
private static String getValue(String key, Properties props) {
return props.getProperty("[" + key + "]");
}
结果如预期:
System.out.println(getValue("123", props));
> "some string"
System.out.println(getValue("234", props));
> 999999999
System.out.println(getValue("345", props));
> "some other string"