getResource和getResourceAsStream读取类资源

本文详细解释了Java中加载资源文件的两种常见方法:getResource。特别强调了资源路径必须使用'/'作为分隔符而非File.separator,并说明了方法的查找顺序及权限要求。
Java里读取资源文件时,经常用到的两个方法,但是经常会用错的方法
方法说明如下
[quote]
public URL getResource(String name)

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

[b]The name of a resource is a '/'-separated path name that identifies the resource.[/b]
[b][u]这句明确说明,name参数是以‘/’来作为路径分隔符,不是File.separator,如果使用了File.separator,正确的路径也返回null[/u][/b]

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

Parameters:
name - The resource name
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
Since:
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值