Class ClassPathResource

java.lang.Object
cloud.opencode.base.classloader.resource.AbstractResource
cloud.opencode.base.classloader.resource.ClassPathResource
All Implemented Interfaces:
Resource

public class ClassPathResource extends AbstractResource
ClassPath Resource - Resource loaded from classpath ClassPath 资源 - 从 classpath 加载的资源

Represents a resource that can be loaded from the classpath.

表示可以从 classpath 加载的资源。

Features | 主要功能:

  • Load resources from classpath - 从 classpath 加载资源
  • Support custom ClassLoader - 支持自定义 ClassLoader
  • Relative resource creation - 相对资源创建

Usage Examples | 使用示例:

Resource resource = new ClassPathResource("config.yml");
Resource resource = new ClassPathResource("config.yml", customClassLoader);
String content = resource.getString();

Security | 安全性:

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

    • ClassPathResource

      public ClassPathResource(String path)
      Create classpath resource with default classloader 使用默认类加载器创建 classpath 资源
      Parameters:
      path - resource path | 资源路径
    • ClassPathResource

      public ClassPathResource(String path, ClassLoader classLoader)
      Create classpath resource with specified classloader 使用指定类加载器创建 classpath 资源
      Parameters:
      path - resource path | 资源路径
      classLoader - class loader | 类加载器
  • Method Details

    • exists

      public boolean exists()
      Description copied from interface: Resource
      Check if resource exists 检查资源是否存在
      Specified by:
      exists in interface Resource
      Overrides:
      exists in class AbstractResource
      Returns:
      true if exists | 存在返回 true
    • getInputStream

      public InputStream getInputStream() throws IOException
      Description copied from interface: Resource
      Get input stream for resource 获取资源的输入流
      Returns:
      input stream | 输入流
      Throws:
      IOException - if cannot open stream | 无法打开流时抛出
    • getUrl

      public URL getUrl() throws IOException
      Description copied from interface: Resource
      Get resource URL 获取资源 URL
      Returns:
      resource URL | 资源 URL
      Throws:
      IOException - if cannot get URL | 无法获取 URL 时抛出
    • getFilename

      public String getFilename()
      Description copied from interface: Resource
      Get filename 获取文件名
      Specified by:
      getFilename in interface Resource
      Overrides:
      getFilename in class AbstractResource
      Returns:
      filename or null | 文件名或 null
    • getDescription

      public String getDescription()
      Description copied from interface: Resource
      Get resource description 获取资源描述
      Returns:
      description | 描述
    • createRelative

      public Resource createRelative(String relativePath) throws IOException
      Description copied from interface: Resource
      Create relative resource 创建相对资源
      Parameters:
      relativePath - relative path | 相对路径
      Returns:
      relative resource | 相对资源
      Throws:
      IOException - if cannot create relative resource | 无法创建相对资源时抛出
    • getResourcePath

      public String getResourcePath()
      Get the resource path 获取资源路径
      Returns:
      resource path | 资源路径
    • getClassLoader

      public ClassLoader getClassLoader()
      Get the class loader 获取类加载器
      Returns:
      class loader | 类加载器