package conf; import java.io.InputStream; import java.util.Properties; public class ReadProp { public static void main(String[] args){ String fileName = "/conf/msf.properties"; Properties props = new Properties(); try{ InputStream in = ReadProp.class.getResourceAsStream(fileName); props.load(in); System.out.println(props.getProperty("renderer.class")); }catch(Exception e){ e.printStackTrace(); } } }
原来getResourceAsStream(String name)中,
name要么是以 "/" 开始的相对路径(相对当前类所在的包);
要么是绝对路径(类似"D:/code/j2se/src/conf/msf.properties")
.难怪刚开始我写成这样
String fileName = "conf/msf.properties";
一直报java.lang.NullPointerException