实际上properties是一个Hashtable的子类,而且实现了map接口,也就是说其实properties当中存储的内容是(key, value)对。而在properties当中存储的内容都是字符串类型。其格式为:
xxx=xxx
可以用properties类来获取当前JVM的系统属性,方式如下:
import java.util.*;
import java.io.*;
class PropTest
{
publicstatic void main(String[] args)
{
Propertiespps=System.getProperties();
pps.list(System.out);
}
}
也可以使用properties类来获取某个配置文件当中的内容,方法如下:
importjava.util.*;
import java.io.*;
class PropTest
{
publicstaticvoid main(String[] args)
{
Properties pps=new Properties();
try
{
pps.load(new FileInputStream("abc.ini"));
Enumeration enum1 =pps.propertyNames();
while(enum1.hasMoreElements())
{
StringstrKey=(String)enum1.nextElement(); //获取key
StringstrValue=pps.getProperty(strKey);
//通过key获取value
System.out.println(strKey);
System.out.println(strValue);
System.out.println(strKey+"="+strValue);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
有几个问题要注意:
1 enum 是保留字,不可以使用!
所以Enumeration enum = pps.propertyNames();是错误的,旧版本很多程序是不可以放到1.6上使用的!
关键是sb eclipse发现不了这个错误,还说Enumeration 有错!