使用J2SE API讀取PropertIEs文件的六種方法
1。使用Java.util.PropertIEs類的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new PropertIEs();
p.load(in);
2。使用Java.util.ResourceBundle類的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用Java.util.PropertyResourceBundle類的構造函數
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class變量的getResourceAsStream()方法
示例:
InputStream in = JPropertIEs.class.getResourceAsStream(name);
Properties p = new PropertIEs();
p.load(in);
5。使用class.getClassLoader()所得到的Java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JPropertIEs.class.getClassLoader().getResourceAsStream(name);
Properties p = new PropertIEs();
p.load(in);
6。使用Java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new PropertIEs();
p.load(in);
補充
Servlet中可以使用Javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new PropertIEs();
p.load(in);