Class OpenResource

java.lang.Object
cloud.opencode.base.io.OpenResource

public final class OpenResource extends Object
Resource Loading Utility Class 资源加载工具类

Utility class for loading resources from classpath, filesystem, and URLs with a unified API.

用于从类路径、文件系统和URL加载资源的工具类,提供统一的API。

Features | 主要功能:

  • Classpath resource loading - 类路径资源加载
  • Filesystem resource loading - 文件系统资源加载
  • URL resource loading - URL资源加载
  • Properties loading - Properties加载

Usage Examples | 使用示例:

// Read classpath resource
String config = OpenResource.readString("application.properties");

// Read properties
Properties props = OpenResource.readProperties("config.properties");

// Check existence
if (OpenResource.exists("optional.xml")) {
    // ...
}

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getResource

      public static URL getResource(String resourceName)
      Gets resource URL 获取资源URL
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      URL or null if not found | URL,未找到返回null
    • getResourceRequired

      public static URL getResourceRequired(String resourceName)
      Gets resource URL (required) 获取资源URL(必须存在)
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      URL | URL
      Throws:
      OpenIOOperationException - if not found | 如果未找到
    • getResources

      public static List<URL> getResources(String resourceName)
      Gets all matching resource URLs 获取所有匹配的资源URL
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      list of URLs | URL列表
    • exists

      public static boolean exists(String resourceName)
      Checks if resource exists 检查资源是否存在
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      true if exists | 如果存在返回true
    • getStream

      public static InputStream getStream(String resourceName)
      Gets resource input stream 获取资源输入流
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      input stream | 输入流
      Throws:
      OpenIOOperationException - if not found | 如果未找到
    • readBytes

      public static byte[] readBytes(String resourceName)
      Reads resource as byte array 读取资源为字节数组
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      byte array | 字节数组
    • readString

      public static String readString(String resourceName, Charset charset)
      Reads resource as string 读取资源为字符串
      Parameters:
      resourceName - the resource name | 资源名称
      charset - the charset | 字符集
      Returns:
      content string | 内容字符串
    • readString

      public static String readString(String resourceName)
      Reads resource as UTF-8 string 读取资源为UTF-8字符串
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      content string | 内容字符串
    • readLines

      public static List<String> readLines(String resourceName, Charset charset)
      Reads resource as lines 读取资源为行列表
      Parameters:
      resourceName - the resource name | 资源名称
      charset - the charset | 字符集
      Returns:
      list of lines | 行列表
    • readLines

      public static List<String> readLines(String resourceName)
      Reads resource as lines with UTF-8 使用UTF-8读取资源为行列表
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      list of lines | 行列表
    • readProperties

      public static Properties readProperties(String resourceName)
      Reads resource as Properties 读取资源为Properties
      Parameters:
      resourceName - the resource name | 资源名称
      Returns:
      Properties object | Properties对象
    • classPathResource

      public static Resource classPathResource(String path)
      Creates a classpath resource 创建类路径资源
      Parameters:
      path - the path | 路径
      Returns:
      resource | 资源
    • fileResource

      public static Resource fileResource(Path path)
      Creates a filesystem resource 创建文件系统资源
      Parameters:
      path - the path | 路径
      Returns:
      resource | 资源
    • fileResource

      public static Resource fileResource(String path)
      Creates a filesystem resource from string 从字符串创建文件系统资源
      Parameters:
      path - the path string | 路径字符串
      Returns:
      resource | 资源
    • urlResource

      public static Resource urlResource(URL url)
      Creates a URL resource 创建URL资源
      Parameters:
      url - the URL | URL
      Returns:
      resource | 资源
    • urlResource

      public static Resource urlResource(String url)
      Creates a URL resource from string 从字符串创建URL资源
      Parameters:
      url - the URL string | URL字符串
      Returns:
      resource | 资源
    • getResourceLoader

      public static ResourceLoader getResourceLoader()
      Gets the resource loader 获取资源加载器
      Returns:
      resource loader | 资源加载器
    • load

      public static Resource load(String location)
      Loads resource using resource loader 使用资源加载器加载资源
      Parameters:
      location - the location (classpath:, file:, or URL) | 位置
      Returns:
      resource | 资源