Interface Resource
- All Known Implementing Classes:
ClassPathResource, FileSystemResource, UrlResource
public interface Resource
Resource Abstraction Interface
资源抽象接口
Unified interface for accessing resources from different sources including classpath, filesystem, and URLs.
统一接口用于访问来自不同来源的资源,包括类路径、文件系统和URL。
Features | 主要功能:
- Unified resource access - 统一资源访问
- Existence and readability checks - 存在性和可读性检查
- Content reading methods - 内容读取方法
- Relative resource creation - 相对资源创建
Usage Examples | 使用示例:
Resource resource = OpenResource.classPathResource("config.properties");
if (resource.exists()) {
String content = resource.readString(StandardCharsets.UTF_8);
}
Security | 安全性:
- Thread-safe: Depends on implementation - 线程安全: 取决于实现
- Null-safe: Depends on implementation - 空值安全: 取决于实现
- Since:
- JDK 25, opencode-base-io V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongGets the content length in bytes 获取内容长度(字节)createRelative(String relativePath) Creates a relative resource 创建相对资源booleanexists()Checks if the resource exists 检查资源是否存在Gets a description of the resource 获取资源描述Gets the filename of the resource 获取资源的文件名Gets the input stream for reading the resource 获取用于读取资源的输入流getPath()Gets the file path if available 获取文件路径(如果可用)getURL()Gets the URL of the resource 获取资源的URLbooleanChecks if the resource is readable 检查资源是否可读longGets the last modified time 获取最后修改时间default byte[]Reads the resource as byte array 读取资源为字节数组default StringReads the resource as string with UTF-8 使用UTF-8读取资源为字符串default StringreadString(Charset charset) Reads the resource as string with specified charset 使用指定字符集读取资源为字符串
-
Method Details
-
exists
boolean exists()Checks if the resource exists 检查资源是否存在- Returns:
- true if exists | 如果存在返回true
-
isReadable
boolean isReadable()Checks if the resource is readable 检查资源是否可读- Returns:
- true if readable | 如果可读返回true
-
getInputStream
InputStream getInputStream()Gets the input stream for reading the resource 获取用于读取资源的输入流- Returns:
- input stream | 输入流
- Throws:
OpenIOOperationException- if the stream cannot be opened | 如果流无法打开
-
getURL
URL getURL()Gets the URL of the resource 获取资源的URL- Returns:
- URL | URL
- Throws:
OpenIOOperationException- if URL is not available | 如果URL不可用
-
getPath
Path getPath()Gets the file path if available 获取文件路径(如果可用)- Returns:
- path or null if not supported | 路径,如果不支持则返回null
-
getDescription
-
getFilename
-
contentLength
long contentLength()Gets the content length in bytes 获取内容长度(字节)- Returns:
- length or -1 if unknown | 长度,如果未知返回-1
-
lastModified
long lastModified()Gets the last modified time 获取最后修改时间- Returns:
- timestamp or -1 if unknown | 时间戳,如果未知返回-1
-
createRelative
Creates a relative resource 创建相对资源- Parameters:
relativePath- the relative path | 相对路径- Returns:
- new resource | 新资源
- Throws:
OpenIOOperationException- if creation fails | 如果创建失败
-
readBytes
default byte[] readBytes()Reads the resource as byte array 读取资源为字节数组- Returns:
- byte array | 字节数组
- Throws:
OpenIOOperationException- if reading fails | 如果读取失败
-
readString
Reads the resource as string with specified charset 使用指定字符集读取资源为字符串- Parameters:
charset- the charset | 字符集- Returns:
- content string | 内容字符串
- Throws:
OpenIOOperationException- if reading fails | 如果读取失败
-
readString
Reads the resource as string with UTF-8 使用UTF-8读取资源为字符串- Returns:
- content string | 内容字符串
- Throws:
OpenIOOperationException- if reading fails | 如果读取失败
-