本文摘机自
Accessing Resources in a Location-Independent Manner
tag: java,resource,location independent
Resource:
/ separated string
/<shortName>.<extension>
System Resource:
CLASSPATH
A resource may be found in a different entry in CLASSPATH than where the class is loaded
Class Loader:
all class loaders search for a resource as system resource firstly
AppletClassLoader - system resource > JAR > CODEBASE > remote
Resouce Name:
replace "." with "/" in fully qualified package name
and add resource name as "<name>.<ext>"
absolute name starts with "/", otherwise it is relative
Manipulating Resource:
return null when not exist or permission denied
use URL.openStream to open the returned URL
use (ImageProducer)URL.getContent to get image stored in the Resource
Security:
return null when in security context addresses existence attacks
ClassLoader will not provide access to the contents of a .class file
no restriction regarding non-system class loader found resource
resource
in directory - getResource() use File.exists() which use checkRead() in the security manager
resource
in JAR - succeed for all files regardless of system or non-system class