在文件中存储以下业务规则的最佳方式是什么,以便可以将它们应用于输入值,即关键字?
Key-INDIA; Value-Delhi.
Key-Australia; Value-Canberra.
Key-Germany, Value-Berlin.一种解决方案:- Xml
<Countries>
<India>Delhi</India>
<Australia>Canberra</Australia>
<Germany>Berlin</Germany>
</Countries>因为规则的数量将大于1000;使用Map实现它是不可能的。
你好,史瑞亚。
发布于 2012-07-10 19:20:36
使用.properties文件并将其存储在键值对中。
India=Delhi.
Australia=Canberra.
Germany=Berlin.并按照hmjd的指示使用java.util.Properties读取该文件。
例如:
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("countries.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("India"));
System.out.println(prop.getProperty("Australia"));
System.out.println(prop.getProperty("Germany"));
} catch (IOException ex) {
ex.printStackTrace();
}发布于 2012-07-10 19:19:41
使用java.util.Properties从文件中写入和读取:
Properties p = new Properties();
p.setProperty("Australia", "Canberra");
p.setProperty("Germany", "Berlin");
File f = new File("my.properties");
FileOutputStream fos = new FileOutputStream(f);
p.store(fos, "my properties");加载后,使用p.load()从文件中读回它们,并使用p.getProperty()查询它们。
发布于 2012-07-10 19:23:29
创建属性文件(如file.properties):
INDIA=Delhi.
Australia=Canberra.
Germany=Berlin.然后在代码中:
public static void main(String[] args) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("file.properties"));
String value= prop.getProperty("INDIA");
...
} catch (Exception e) {
}
}https://stackoverflow.com/questions/11412140
复制相似问题