Class ResourceFile

java.lang.Object
com.oorian.utils.OorianFile
com.oorian.utils.ResourceFile

public class ResourceFile extends OorianFile
A read-only file backed by a classpath or JAR resource.

Uses the current thread's context ClassLoader to locate resources. The leading / is stripped from the path if present, since ClassLoader.getResourceAsStream() expects paths without a leading slash.

Usage example:


 ResourceFile config = new ResourceFile("oorian.properties");
 if (config.exists())
 {
     String contents = config.read();
 }
 
See Also:
  • Constructor Details

    • ResourceFile

      public ResourceFile(String path)
      Creates a new ResourceFile for the given classpath resource path.

      A leading / is automatically stripped for ClassLoader compatibility.

      Parameters:
      path - the classpath resource path (e.g., "oorian.properties" or "/com/oorian/defaults.xml").
  • Method Details

    • exists

      public static boolean exists(String path)
      Returns whether a classpath resource exists at the given path.
      Parameters:
      path - the classpath resource path (e.g., "oorian.properties" or "/com/oorian/defaults.xml").
      Returns:
      true if the resource exists, false otherwise.
    • read

      public static String read(String path) throws IOException
      Reads the entire contents of a classpath resource as a UTF-8 string.
      Parameters:
      path - the classpath resource path (e.g., "oorian.properties" or "/com/oorian/defaults.xml").
      Returns:
      the resource contents as a string.
      Throws:
      IOException - if the resource does not exist or an I/O error occurs.
    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns an InputStream for reading this file's contents.
      Specified by:
      getInputStream in class OorianFile
      Returns:
      an input stream for this file.
      Throws:
      IOException - if the file cannot be opened or does not exist.
    • exists

      public boolean exists()
      Returns whether this file exists and is accessible.
      Specified by:
      exists in class OorianFile
      Returns:
      true if the file exists, false otherwise.
    • getPath

      public String getPath()
      Returns the path of this file as provided at construction time.
      Specified by:
      getPath in class OorianFile
      Returns:
      the file path.