Properties是JAVA比较老的一个特性,而且几乎没有做过什么变动。

过去对于读取Properties很少关注过JAVA Properties这个类,今天试了一下却发现真的很好用,直接贴我的代码了:
[JAVA]
public String getPro(String filename,String key)
{
String returnvalue = null;
try {
FileInputStream fis = new FileInputStream(“./config/”+filename);
Properties pro = new Properties();
pro.load(fis);
returnvalue = pro.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
}
return returnvalue;
}
[/JAVA]
这是实现代码,同时我写了一个测试类:
[JAVA]
public void test()
{
GetPro gpro = new GetPro();
String name = gpro.getPro(“config.ini”,“name”);
System.out.println(name);
}
[/JAVA]
config.ini:
name = helloworld
测试结果是:
helloworld
很简单吧~

 

 

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注