public class ResourceUtil extends Object
Constructor and Description |
---|
ResourceUtil() |
Modifier and Type | Method and Description |
---|---|
static BufferedReader |
getReader(String resource,
Charset charset)
从ClassPath资源中获取
BufferedReader |
static Resource |
getResource(File file)
获取
FileResource 资源对象 |
static Resource |
getResource(String path)
|
static Resource |
getResource(URL url)
获取
UrlResource 资源对象 |
static MultiResource |
getResources(String resource)
获取同名的所有资源
|
static MultiResource |
getResources(String resource,
ClassLoader classLoader)
获取同名的所有资源
资源的加载顺序是: 1. |
static URL |
getResourceUrl(String resource)
获得资源的URL
路径用/分隔,例如: config/a/db.config spring/xml/test.xml |
static URL |
getResourceUrl(String resource,
Class<?> baseClass)
获得资源相对路径对应的URL
|
static EnumerationIter<URL> |
getResourceUrlIter(String resource)
获取指定路径下的资源Iterator
路径格式必须为目录格式,用/分隔,例如: config/a spring/xml |
static EnumerationIter<URL> |
getResourceUrlIter(String resource,
ClassLoader classLoader)
获取指定路径下的资源Iterator
路径格式必须为目录格式,用/分隔,例如: config/a spring/xml |
static List<URL> |
getResourceUrls(String resource)
获取指定路径下的资源列表
路径格式必须为目录格式,用/分隔,例如: config/a spring/xml |
static List<URL> |
getResourceUrls(String resource,
Predicate<URL> filter)
获取指定路径下的资源列表
路径格式必须为目录格式,用/分隔,例如: config/a spring/xml |
static InputStream |
getStream(String resource)
从ClassPath资源中获取
InputStream |
static InputStream |
getStreamSafe(String resource)
从ClassPath资源中获取
InputStream ,当资源不存在时返回null |
static BufferedReader |
getUtf8Reader(String resource)
从ClassPath资源中获取
BufferedReader |
static void |
loadAllTo(Properties properties,
String resourceName,
ClassLoader classLoader,
Charset charset,
boolean isOverride)
加载指定名称的所有配置文件内容到
Properties 中 |
static void |
loadTo(Properties properties,
Resource resource,
Charset charset)
|
static byte[] |
readBytes(String resource)
读取Classpath下的资源为byte[]
|
static String |
readStr(String resource,
Charset charset)
读取Classpath下的资源为字符串
|
static String |
readUtf8Str(String resource)
读取Classpath下的资源为字符串,使用UTF-8编码
|
public static String readUtf8Str(String resource)
resource
- 资源路径,使用相对ClassPath的路径public static String readStr(String resource, Charset charset)
resource
- 可以是绝对路径,也可以是相对路径(相对ClassPath)charset
- 编码public static byte[] readBytes(String resource)
resource
- 可以是绝对路径,也可以是相对路径(相对ClassPath)public static InputStream getStream(String resource) throws NoResourceException
InputStream
resource
- ClassPath资源InputStream
NoResourceException
- 资源不存在异常public static InputStream getStreamSafe(String resource)
InputStream
,当资源不存在时返回nullresource
- ClassPath资源InputStream
public static BufferedReader getUtf8Reader(String resource)
BufferedReader
resource
- ClassPath资源InputStream
public static BufferedReader getReader(String resource, Charset charset)
BufferedReader
resource
- ClassPath资源charset
- 编码InputStream
public static URL getResourceUrl(String resource) throws IORuntimeException
config/a/db.config spring/xml/test.xml
resource
- 资源(相对Classpath的路径)IORuntimeException
- IO异常public static List<URL> getResourceUrls(String resource)
config/a spring/xml
resource
- 资源路径public static List<URL> getResourceUrls(String resource, Predicate<URL> filter)
config/a spring/xml
resource
- 资源路径filter
- 过滤器,用于过滤不需要的资源,null
表示不过滤,保留所有元素public static EnumerationIter<URL> getResourceUrlIter(String resource)
config/a spring/xml
resource
- 资源路径public static EnumerationIter<URL> getResourceUrlIter(String resource, ClassLoader classLoader)
config/a spring/xml
resource
- 资源路径classLoader
- ClassLoader
public static URL getResourceUrl(String resource, Class<?> baseClass)
resource
- 资源相对路径,null
和""都表示classpath根路径baseClass
- 基准Class,获得的相对路径相对于此Class所在路径,如果为null
则相对ClassPathURL
public static Resource getResource(String path)
path
- 路径,可以是绝对路径,也可以是相对路径(相对ClassPath)Resource
资源对象public static Resource getResource(URL url)
UrlResource
资源对象url
- URLResource
资源对象public static Resource getResource(File file)
FileResource
资源对象public static MultiResource getResources(String resource)
resource
- 资源名MultiResource
public static MultiResource getResources(String resource, ClassLoader classLoader)
resource
- 资源名classLoader
- ClassLoader
,null
表示使用默认的当前上下文ClassLoaderMultiResource
public static void loadTo(Properties properties, Resource resource, Charset charset)
properties
- Properties
文件resource
- 资源charset
- 编码,对XML无效,默认UTF-8public static void loadAllTo(Properties properties, String resourceName, ClassLoader classLoader, Charset charset, boolean isOverride)
Properties
中properties
- Properties
文件resourceName
- 资源名,可以是相对classpath的路径,也可以是绝对路径classLoader
- ClassLoader
,null
表示使用默认的当前上下文ClassLoadercharset
- 编码,对XML无效,默认UTF-8isOverride
- 是否覆盖模式Copyright © 2025. All rights reserved.