Properties 是java自带的读取配置文件的工具类。
/com/test/properties/test.properties 是一个准备好的properties文件,内容如下:
hello_1=world1
hello_2=world2
hello_3=world3
hello_4=world4
hello_5=world5
hello_6=world6
hello_7=world7
hello_8=world8
hello_9=world9
hello_0=world0
java的源码如下:
public class PropertiesTest {
/**
* @param args
*/
public static void main(String[] args) {
PropertiesTest test = new PropertiesTest();
test.setProperty();
}
private void setProperty() {
String fileName = "/com/test/properties/test.properties";
InputStream in = PropertiesTest.class.getResourceAsStream(fileName);
Properties properties = new Properties();
try {
properties.load(in);
// 读取出所有properties的内容,并设置为系统变量
// Register the properties as system properties
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
String value = properties.getProperty(name);
if (value != null) {
System.out.println(name + "=" + value);
System.setProperty(name, value);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}