Package com.oorian.utils
Class ResourceFile
java.lang.Object
com.oorian.utils.OorianFile
com.oorian.utils.ResourceFile
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 Summary
ConstructorsConstructorDescriptionResourceFile(String path) Creates a newResourceFilefor the given classpath resource path. -
Method Summary
Modifier and TypeMethodDescriptionbooleanexists()Returns whether this file exists and is accessible.static booleanReturns whether a classpath resource exists at the given path.Returns anInputStreamfor reading this file's contents.getPath()Returns the path of this file as provided at construction time.static StringReads the entire contents of a classpath resource as a UTF-8 string.Methods inherited from class com.oorian.utils.OorianFile
getName, read, readAsBytes, readAsLines, readAsString
-
Constructor Details
-
ResourceFile
Creates a newResourceFilefor the given classpath resource path.A leading
/is automatically stripped forClassLoadercompatibility.- Parameters:
path- the classpath resource path (e.g.,"oorian.properties"or"/com/oorian/defaults.xml").
-
-
Method Details
-
exists
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:
trueif the resource exists,falseotherwise.
-
read
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
Returns anInputStreamfor reading this file's contents.- Specified by:
getInputStreamin classOorianFile- 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:
existsin classOorianFile- Returns:
trueif the file exists,falseotherwise.
-
getPath
Returns the path of this file as provided at construction time.- Specified by:
getPathin classOorianFile- Returns:
- the file path.
-