一行代码引用资源文件

来源:互联网 发布:dota2怎么公开数据 编辑:程序博客网 时间:2024/04/30 04:34

以前不知道有这么个类,资源文件的引用都是自己写的方法:
java.util.ResourceBundle.getBundle("com/frame/webFrame/portal").getString("RequisitionAction.optical")

ResourceBundle是个抽象类,getBundle是个工厂方法,入参即为资源文件所在相对路径,
该相对路径是相对于classes文件夹而言的,也可以用com.frame.webFrame.portal,这点切记。
该方法返回一个ResourceBundle对象,然后通过其实例方法,入参为key-value中的key,由此即可得到Value

方法2:

Properties p= new Properties();
InputStream i = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/archermind/wep/it/outer/seq.properties");
   p.load(i);

property.load(new FileInputStream("com.mysave.properties"));
路径也是相对于classes文件夹而言的,可以测试一下。

原创粉丝点击