Class ClassPathResource

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

public final class ClassPathResource extends Object implements Resource
Classpath Resource Implementation 类路径资源实现

Resource implementation for resources loaded from classpath. Uses ClassLoader to locate and load resources.

用于从类路径加载资源的资源实现。 使用ClassLoader定位和加载资源。

Features | 主要功能:

  • Load resources from classpath - 从类路径加载资源
  • Support custom ClassLoader - 支持自定义ClassLoader
  • Immutable and thread-safe - 不可变且线程安全

Usage Examples | 使用示例:

ClassPathResource resource = new ClassPathResource("config/app.properties");
String content = resource.readString();
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ClassPathResource

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

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

    • exists

      public boolean exists()
      Description copied from interface: Resource
      Checks if the resource exists 检查资源是否存在
      Specified by:
      exists in interface Resource
      Returns:
      true if exists | 如果存在返回true
    • isReadable

      public boolean isReadable()
      Description copied from interface: Resource
      Checks if the resource is readable 检查资源是否可读
      Specified by:
      isReadable in interface Resource
      Returns:
      true if readable | 如果可读返回true
    • getInputStream

      public InputStream getInputStream()
      Description copied from interface: Resource
      Gets the input stream for reading the resource 获取用于读取资源的输入流
      Specified by:
      getInputStream in interface Resource
      Returns:
      input stream | 输入流
    • getURL

      public URL getURL()
      Description copied from interface: Resource
      Gets the URL of the resource 获取资源的URL
      Specified by:
      getURL in interface Resource
      Returns:
      URL | URL
    • getPath

      public Path getPath()
      Description copied from interface: Resource
      Gets the file path if available 获取文件路径(如果可用)
      Specified by:
      getPath in interface Resource
      Returns:
      path or null if not supported | 路径,如果不支持则返回null
    • getDescription

      public String getDescription()
      Description copied from interface: Resource
      Gets a description of the resource 获取资源描述
      Specified by:
      getDescription in interface Resource
      Returns:
      description | 描述
    • getFilename

      public String getFilename()
      Description copied from interface: Resource
      Gets the filename of the resource 获取资源的文件名
      Specified by:
      getFilename in interface Resource
      Returns:
      filename | 文件名
    • contentLength

      public long contentLength()
      Description copied from interface: Resource
      Gets the content length in bytes 获取内容长度(字节)
      Specified by:
      contentLength in interface Resource
      Returns:
      length or -1 if unknown | 长度,如果未知返回-1
    • lastModified

      public long lastModified()
      Description copied from interface: Resource
      Gets the last modified time 获取最后修改时间
      Specified by:
      lastModified in interface Resource
      Returns:
      timestamp or -1 if unknown | 时间戳,如果未知返回-1
    • createRelative

      public Resource createRelative(String relativePath)
      Description copied from interface: Resource
      Creates a relative resource 创建相对资源
      Specified by:
      createRelative in interface Resource
      Parameters:
      relativePath - the relative path | 相对路径
      Returns:
      new resource | 新资源
    • getClassPath

      public String getClassPath()
      Gets the classpath 获取类路径
      Returns:
      path | 路径
    • getClassLoader

      public ClassLoader getClassLoader()
      Gets the classloader 获取类加载器
      Returns:
      classloader | 类加载器
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object