转载:[url]http://www.gold98.net/blog/article.asp?id=776[/url]
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
/**
* @author alex 大灰狼 from 梦想博客
*
*/
public class TestReadPropFile {
public static void main(String[] args) {
InputStream inputStream = null;
try {
String fileName = "/config.properties";
inputStream = TestReadPropFile.class.getResourceAsStream(fileName);
Properties prop = new Properties();
prop.load(inputStream);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();) {
String key = (String) it.next();
String value=(String) prop.get(key);
System.out.println("key: "+key+" value: "+value);
}
System.out.println(prop.getProperty("CostExpress"));
System.out.println(prop.getProperty("path"));
inputStream.close();
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
}
TestReadPropFile.class在非静态方法里可以用getClass()替换!