Interface Resource
- All Known Implementing Classes:
AbstractResource, ByteArrayResource, ClassPathResource, FileResource, InputStreamResource, JarResource, UrlResource
public interface Resource
Resource Interface - Unified resource access abstraction
资源接口 - 统一的资源访问抽象
Abstract interface for resource access from different sources (classpath, file, URL, JAR).
用于从不同来源(classpath、文件、URL、JAR)访问资源的抽象接口。
Features | 主要功能:
- Existence check - 存在性检查
- Content reading - 内容读取
- Metadata access - 元数据访问
- Relative resource creation - 相对资源创建
Usage Examples | 使用示例:
Resource resource = new ClassPathResource("config.yml");
if (resource.exists()) {
String content = resource.getString();
}
Security | 安全性:
- Thread-safe: Depends on implementation - 线程安全: 取决于实现
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-classloader V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongGet content length in bytes 获取内容长度(字节)createRelative(String relativePath) Create relative resource 创建相对资源booleanexists()Check if resource exists 检查资源是否存在default byte[]getBytes()Read resource as byte array 读取资源为字节数组Get resource description 获取资源描述getFile()Get file if resource is file-based 获取文件(如果是文件资源)Get filename 获取文件名Get input stream for resource 获取资源的输入流getPath()Get path if resource is file-based 获取路径(如果是文件资源)default StringRead resource as string (UTF-8) 读取资源为字符串(UTF-8)default StringRead resource as string with specified charset 使用指定字符集读取资源为字符串getUri()Get resource URI 获取资源 URIgetUrl()Get resource URL 获取资源 URLbooleanisFile()Check if resource is a file 检查资源是否为文件booleanCheck if resource is readable 检查资源是否可读longGet last modified timestamp 获取最后修改时间戳Read all lines (UTF-8) 读取所有行(UTF-8)Read all lines with specified charset 使用指定字符集读取所有行
-
Method Details
-
exists
boolean exists()Check if resource exists 检查资源是否存在- Returns:
- true if exists | 存在返回 true
-
isReadable
boolean isReadable()Check if resource is readable 检查资源是否可读- Returns:
- true if readable | 可读返回 true
-
isFile
boolean isFile()Check if resource is a file 检查资源是否为文件- Returns:
- true if is file | 是文件返回 true
-
getInputStream
Get input stream for resource 获取资源的输入流- Returns:
- input stream | 输入流
- Throws:
IOException- if cannot open stream | 无法打开流时抛出
-
getUrl
Get resource URL 获取资源 URL- Returns:
- resource URL | 资源 URL
- Throws:
IOException- if cannot get URL | 无法获取 URL 时抛出
-
getUri
Get resource URI 获取资源 URI- Returns:
- resource URI | 资源 URI
- Throws:
IOException- if cannot get URI | 无法获取 URI 时抛出
-
getFile
-
getPath
-
contentLength
Get content length in bytes 获取内容长度(字节)- Returns:
- content length or -1 if unknown | 内容长度,未知时返回 -1
- Throws:
IOException- if cannot determine length | 无法确定长度时抛出
-
lastModified
Get last modified timestamp 获取最后修改时间戳- Returns:
- last modified time in milliseconds or 0 if unknown | 最后修改时间(毫秒),未知时返回 0
- Throws:
IOException- if cannot determine time | 无法确定时间时抛出
-
getFilename
-
getDescription
-
getBytes
Read resource as byte array 读取资源为字节数组- Returns:
- byte array | 字节数组
- Throws:
IOException- if read fails | 读取失败时抛出
-
getString
Read resource as string (UTF-8) 读取资源为字符串(UTF-8)- Returns:
- content as string | 字符串内容
- Throws:
IOException- if read fails | 读取失败时抛出
-
getString
Read resource as string with specified charset 使用指定字符集读取资源为字符串- Parameters:
charset- character set | 字符集- Returns:
- content as string | 字符串内容
- Throws:
IOException- if read fails | 读取失败时抛出
-
readLines
Read all lines (UTF-8) 读取所有行(UTF-8)- Returns:
- list of lines | 行列表
- Throws:
IOException- if read fails | 读取失败时抛出
-
readLines
Read all lines with specified charset 使用指定字符集读取所有行- Parameters:
charset- character set | 字符集- Returns:
- list of lines | 行列表
- Throws:
IOException- if read fails | 读取失败时抛出
-
createRelative
Create relative resource 创建相对资源- Parameters:
relativePath- relative path | 相对路径- Returns:
- relative resource | 相对资源
- Throws:
IOException- if cannot create relative resource | 无法创建相对资源时抛出
-