Class ResourceLoader
java.lang.Object
cloud.opencode.base.test.ResourceLoader
Resource Loader - Test resource loading utility
资源加载器 - 测试资源加载工具
Utility class for loading test resources from classpath or filesystem.
用于从类路径或文件系统加载测试资源的工具类。
Features | 主要功能:
- Load resources from classpath - 从类路径加载资源
- Load resources from filesystem - 从文件系统加载资源
- Support for text, bytes, properties - 支持文本、字节、属性文件
- Support for different charsets - 支持不同字符集
Usage Examples | 使用示例:
// Load text file from classpath
String content = ResourceLoader.loadString("test-data.json");
// Load as lines
List<String> lines = ResourceLoader.loadLines("test-data.txt");
// Load properties
Properties props = ResourceLoader.loadProperties("test-config.properties");
// Load bytes
byte[] bytes = ResourceLoader.loadBytes("test-image.png");
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes (throws IllegalArgumentException for null/missing resources) - 空值安全: 是(对空值/缺失资源抛出IllegalArgumentException)
- Since:
- JDK 25, opencode-base-test V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanChecks if a resource exists.static InputStreamgetResourceStream(String resourcePath) Gets an InputStream for the resource.getResourceURL(String resourcePath) Gets the URL for the resource.static byte[]Loads a resource as a byte array.static StringLoads a file from filesystem as string.static StringLoads a file from filesystem as string with charset.static byte[]loadFileBytes(Path path) Loads file bytes from filesystem.loadFileLines(Path path) Loads file lines from filesystem.Loads a resource as a list of lines.Loads a resource as a list of lines with specified charset.static PropertiesloadProperties(String resourcePath) Loads a properties file.static StringloadString(String resourcePath) Loads a resource as a string using UTF-8 encoding.static StringloadString(String resourcePath, Charset charset) Loads a resource as a string with specified charset.loadStringOptional(String resourcePath) Loads a resource as a string, returning Optional.
-
Method Details
-
loadString
Loads a resource as a string using UTF-8 encoding. 使用 UTF-8 编码将资源加载为字符串。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- the content as string | 内容字符串
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
loadString
Loads a resource as a string with specified charset. 使用指定字符集将资源加载为字符串。- Parameters:
resourcePath- the resource path | 资源路径charset- the charset | 字符集- Returns:
- the content as string | 内容字符串
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
loadStringOptional
-
loadLines
Loads a resource as a list of lines. 将资源加载为行列表。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- the lines | 行列表
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
loadLines
Loads a resource as a list of lines with specified charset. 使用指定字符集将资源加载为行列表。- Parameters:
resourcePath- the resource path | 资源路径charset- the charset | 字符集- Returns:
- the lines | 行列表
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
loadBytes
Loads a resource as a byte array. 将资源加载为字节数组。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- the bytes | 字节数组
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
loadProperties
Loads a properties file. 加载属性文件。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- the properties | 属性
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
getResourceStream
Gets an InputStream for the resource. 获取资源的输入流。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- the input stream | 输入流
- Throws:
IllegalArgumentException- if resource not found | 如果资源未找到
-
getResourceURL
-
exists
Checks if a resource exists. 检查资源是否存在。- Parameters:
resourcePath- the resource path | 资源路径- Returns:
- true if exists | 如果存在返回 true
-
loadFile
Loads a file from filesystem as string. 从文件系统加载文件为字符串。- Parameters:
path- the file path | 文件路径- Returns:
- the content | 内容
- Throws:
IllegalArgumentException- if file not found | 如果文件未找到
-
loadFile
Loads a file from filesystem as string with charset. 使用字符集从文件系统加载文件为字符串。- Parameters:
path- the file path | 文件路径charset- the charset | 字符集- Returns:
- the content | 内容
- Throws:
IllegalArgumentException- if file not found | 如果文件未找到
-
loadFileLines
Loads file lines from filesystem. 从文件系统加载文件行。- Parameters:
path- the file path | 文件路径- Returns:
- the lines | 行列表
- Throws:
IllegalArgumentException- if file not found | 如果文件未找到
-
loadFileBytes
Loads file bytes from filesystem. 从文件系统加载文件字节。- Parameters:
path- the file path | 文件路径- Returns:
- the bytes | 字节数组
- Throws:
IllegalArgumentException- if file not found | 如果文件未找到
-