public class ResourceUtil extends Object
1.ClassLoader 不指定contextClass时,优先使用Thread.getContextClassLoader(), 如果ContextClassLoader未设置则使用Guava Resources类的ClassLoader 指定contextClass时,则直接使用该contextClass的ClassLoader.
2.路径 不指定contextClass时,按URLClassLoader的实现, 从jar file中查找resourceName, 所以resourceName无需以"/"打头即表示jar file中的根目录,带了"/" 反而导致JarFile.getEntry(resouceName)时没有返回. 指定contextClass时,class.getResource()会先对name进行处理再交给classLoader,打头的"/"的会被去除,不以"/"打头则表示与该contextClass package的相对路径, 会先转为绝对路径.
3.同名资源 如果有多个同名资源,除非调用getResources()获取全部资源,否则在URLClassLoader中按ClassPath顺序打开第一个命中的Jar文件.
| Constructor and Description |
|---|
ResourceUtil() |
| Modifier and Type | Method and Description |
|---|---|
static InputStream |
asStream(Class<?> contextClass,
String resourceName)
读取文件的每一行,读取规则见本类注释.
|
static InputStream |
asStream(String resourceName)
读取规则见本类注释.
|
static URL |
asUrl(Class<?> contextClass,
String resourceName)
读取规则见本类注释.
|
static URL |
asUrl(String resourceName)
读取规则见本类注释.
|
static List<URL> |
getResourcesQuietly(String resourceName) |
static List<URL> |
getResourcesQuietly(String resourceName,
ClassLoader contextClassLoader) |
static List<String> |
toLines(Class<?> contextClass,
String resourceName)
读取文件的每一行,读取规则见本类注释.
|
static List<String> |
toLines(String resourceName)
读取文件的每一行,读取规则见本类注释.
|
static String |
toString(Class<?> contextClass,
String resourceName)
读取文件的每一行,读取规则见本类注释.
|
static String |
toString(String resourceName)
读取文件的每一行,读取规则见本类注释.
|
public static InputStream asStream(String resourceName) throws IOException
IOExceptionpublic static InputStream asStream(Class<?> contextClass, String resourceName) throws IOException
IOExceptionpublic static String toString(String resourceName) throws IOException
IOExceptionpublic static String toString(Class<?> contextClass, String resourceName) throws IOException
IOExceptionpublic static List<String> toLines(String resourceName) throws IOException
IOExceptionpublic static List<String> toLines(Class<?> contextClass, String resourceName) throws IOException
IOExceptionpublic static List<URL> getResourcesQuietly(String resourceName, ClassLoader contextClassLoader)
Copyright © 2018. All rights reserved.