Class FileResource

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

public class FileResource extends AbstractResource
File Resource - Resource from file system 文件资源 - 从文件系统加载的资源

Represents a resource that exists on the file system.

表示存在于文件系统上的资源。

Features | 主要功能:

  • Load resources from file system - 从文件系统加载资源
  • Support File and Path - 支持 File 和 Path
  • File metadata access - 文件元数据访问

Usage Examples | 使用示例:

Resource resource = new FileResource("/etc/config.yml");
Resource resource = new FileResource(new File("/etc/config.yml"));
Resource resource = new FileResource(Path.of("/etc/config.yml"));

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

    • FileResource

      public FileResource(String path)
      Create file resource from path string 从路径字符串创建文件资源
      Parameters:
      path - file path string | 文件路径字符串
    • FileResource

      public FileResource(File file)
      Create file resource from File 从 File 创建文件资源
      Parameters:
      file - file object | 文件对象
    • FileResource

      public FileResource(Path path)
      Create file resource from Path 从 Path 创建文件资源
      Parameters:
      path - path object | 路径对象
  • 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
    • isReadable

      public boolean isReadable()
      Description copied from interface: Resource
      Check if resource is readable 检查资源是否可读
      Specified by:
      isReadable in interface Resource
      Overrides:
      isReadable in class AbstractResource
      Returns:
      true if readable | 可读返回 true
    • isFile

      public boolean isFile()
      Description copied from interface: Resource
      Check if resource is a file 检查资源是否为文件
      Specified by:
      isFile in interface Resource
      Overrides:
      isFile in class AbstractResource
      Returns:
      true if is file | 是文件返回 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 时抛出
    • getUri

      public URI getUri() throws IOException
      Description copied from interface: Resource
      Get resource URI 获取资源 URI
      Specified by:
      getUri in interface Resource
      Overrides:
      getUri in class AbstractResource
      Returns:
      resource URI | 资源 URI
      Throws:
      IOException - if cannot get URI | 无法获取 URI 时抛出
    • getFile

      public Optional<File> getFile()
      Description copied from interface: Resource
      Get file if resource is file-based 获取文件(如果是文件资源)
      Specified by:
      getFile in interface Resource
      Overrides:
      getFile in class AbstractResource
      Returns:
      optional file | 可选的文件
    • getPath

      public Optional<Path> getPath()
      Description copied from interface: Resource
      Get path if resource is file-based 获取路径(如果是文件资源)
      Specified by:
      getPath in interface Resource
      Overrides:
      getPath in class AbstractResource
      Returns:
      optional path | 可选的路径
    • contentLength

      public long contentLength() throws IOException
      Description copied from interface: Resource
      Get content length in bytes 获取内容长度(字节)
      Specified by:
      contentLength in interface Resource
      Overrides:
      contentLength in class AbstractResource
      Returns:
      content length or -1 if unknown | 内容长度,未知时返回 -1
      Throws:
      IOException - if cannot determine length | 无法确定长度时抛出
    • lastModified

      public long lastModified() throws IOException
      Description copied from interface: Resource
      Get last modified timestamp 获取最后修改时间戳
      Specified by:
      lastModified in interface Resource
      Overrides:
      lastModified in class AbstractResource
      Returns:
      last modified time in milliseconds or 0 if unknown | 最后修改时间(毫秒),未知时返回 0
      Throws:
      IOException - if cannot determine time | 无法确定时间时抛出
    • 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 | 无法创建相对资源时抛出
    • getFilePath

      public Path getFilePath()
      Get the file path 获取文件路径
      Returns:
      file path | 文件路径