Properties文件加载方式

MethodParameter formatLookup failure behaviorUsage example
ClassLoader.
getResourceAsStream()
"/"-separated names; no leading "/" (all names are absolute)Silent (returns null) this.getClass().getClassLoader()
.getResourceAsStream
("some/pkg/resource.properties")
Class.
getResourceAsStream()
"/"-separated names; leading "/" indicates absolute names; all other names are relative to the class's packageSilent (returns null) this.getClass()
.getResourceAsStream
("resource.properties")
ResourceBundle.
getBundle()
"."-separated names; all names are absolute; .properties suffix is implied Throws unchecked
java.util.MissingResourceException
ResourceBundle.getBundle
("some.pkg.resource")
这个是取读取资源文件的几种方法。
第一种方法:通过ClassLoader去加载资源。参数需要从classpath的入口来算起。也就是说需要取全路径名称。
第二种方法:通过Class来加载。这种参数,前面的package已经知道了,所以,前面这一部分就已经省去了,只需要写后面的部分就可以了。
第三种方法:通过ResourceBundle来加载。和ClassLoader一样,不过参数是用.来分割的,而不是用/来分割的:)

在实际中,根据资源文件打包位置的不同,选择不同的方法。
在我的程序中:由于sql文件和class是在同一个package下,可以选择第二种方法。当然,也可以选择第一种方法。我就是这么弄的。

如果sql和class不在同一个package下,那么,最好还是使用ClassLoader来加载,也就是第一种方法。

嘿嘿,不错。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值