Class ResourceKit

java.lang.Object
org.miaixz.bus.core.xyz.ResourceKit

public class ResourceKit extends Object
Resource资源工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • ResourceKit

      public ResourceKit()
  • Method Details

    • readString

      public static String readString(String resource)
      读取Classpath下的资源为字符串,使用UTF-8编码
      Parameters:
      resource - 资源路径,使用相对ClassPath的路径
      Returns:
      资源内容
    • readString

      public static String readString(String resource, Charset charset)
      读取Classpath下的资源为字符串
      Parameters:
      resource - 可以是绝对路径,也可以是相对路径(相对ClassPath)
      charset - 编码
      Returns:
      资源内容
    • readBytes

      public static byte[] readBytes(String resource)
      读取Classpath下的资源为byte[]
      Parameters:
      resource - 可以是绝对路径,也可以是相对路径(相对ClassPath)
      Returns:
      资源内容
    • getStream

      public static InputStream getStream(String resource) throws InternalException
      从ClassPath资源中获取InputStream
      Parameters:
      resource - ClassPath资源
      Returns:
      InputStream
      Throws:
      InternalException - 资源不存在异常
    • getStreamSafe

      public static InputStream getStreamSafe(String resource)
      从ClassPath资源中获取InputStream,当资源不存在时返回null
      Parameters:
      resource - ClassPath资源
      Returns:
      InputStream
    • getReader

      public static BufferedReader getReader(String resource)
      从ClassPath资源中获取BufferedReader
      Parameters:
      resource - ClassPath资源
      Returns:
      InputStream
    • getReader

      public static BufferedReader getReader(String resource, Charset charset)
      从ClassPath资源中获取BufferedReader
      Parameters:
      resource - ClassPath资源
      charset - 编码
      Returns:
      InputStream
    • getResourceUrl

      public static URL getResourceUrl(String resource) throws InternalException
      获得资源的URL 路径用/分隔,例如:
       config/a/db.config
       spring/xml/test.xml
       
      Parameters:
      resource - 资源(相对Classpath的路径)
      Returns:
      资源URL
      Throws:
      InternalException - IO异常
    • getResourceUrls

      public static List<URL> getResourceUrls(String resource)
      获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      Returns:
      资源列表
    • getResourceUrls

      public static List<URL> getResourceUrls(String resource, Predicate<URL> filter)
      获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      filter - 过滤器,用于过滤不需要的资源,null表示不过滤,保留所有元素
      Returns:
      资源列表
    • getResourceUrlIter

      public static EnumerationIterator<URL> getResourceUrlIter(String resource)
      获取指定路径下的资源Iterator 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      Returns:
      资源列表
    • getResourceUrlIter

      public static EnumerationIterator<URL> getResourceUrlIter(String resource, ClassLoader classLoader)
      获取指定路径下的资源Iterator 路径格式必须为目录格式,用/分隔,例如:
       config/a
       spring/xml
       
      Parameters:
      resource - 资源路径
      classLoader - ClassLoader
      Returns:
      资源列表
    • getResourceUrl

      public static URL getResourceUrl(String resource, Class<?> baseClass)
      获得资源相对路径对应的URL
      Parameters:
      resource - 资源相对路径,null和""都表示classpath根路径
      baseClass - 基准Class,获得的相对路径相对于此Class所在路径,如果为null则相对ClassPath
      Returns:
      URL
    • getResource

      public static Resource getResource(String path)
      获取Resource 资源对象 如果提供路径为绝对路径或路径以file:开头,返回FileResource,否则返回ClassPathResource
      Parameters:
      path - 路径,可以是绝对路径,也可以是相对路径(相对ClassPath)
      Returns:
      Resource 资源对象
    • getResource

      public static Resource getResource(URL url)
      获取UrlResource 资源对象
      Parameters:
      url - URL
      Returns:
      Resource 资源对象
    • getResource

      public static Resource getResource(File file)
      获取FileResource 资源对象
      Parameters:
      file - File
      Returns:
      Resource 资源对象
    • getResources

      public static MultiResource getResources(String resource)
      获取同名的所有资源
      Parameters:
      resource - 资源名
      Returns:
      MultiResource
    • getResources

      public static MultiResource getResources(String resource, ClassLoader classLoader)
      获取同名的所有资源 资源的加载顺序是:
      • 1. 首先在本项目下查找资源文件
      • 2. 按照classpath定义的顺序,去对应路径或jar包下寻找资源文件
      Parameters:
      resource - 资源名
      classLoader - ClassLoadernull表示使用默认的当前上下文ClassLoader
      Returns:
      MultiResource
    • loadTo

      public static void loadTo(Properties properties, Resource resource, Charset charset)
      加载配置文件内容到Properties中 需要注意的是,如果资源文件的扩展名是.xml,会调用Properties.loadFromXML(InputStream) 读取。
      Parameters:
      properties - Properties文件
      resource - 资源
      charset - 编码,对XML无效,默认UTF-8
    • loadAllTo

      public static void loadAllTo(Properties properties, String resourceName, ClassLoader classLoader, Charset charset, boolean isOverride)
      加载指定名称的所有配置文件内容到Properties
      Parameters:
      properties - Properties文件
      resourceName - 资源名,可以是相对classpath的路径,也可以是绝对路径
      classLoader - ClassLoadernull表示使用默认的当前上下文ClassLoader
      charset - 编码,对XML无效,默认UTF-8
      isOverride - 是否覆盖模式