Package org.daiitech.naftah.utils
Class ResourceUtils
java.lang.Object
org.daiitech.naftah.utils.ResourceUtils
Utility class for handling resources such as reading files,
locating the JAR directory, opening streams from URLs,
and loading properties files.
This class cannot be instantiated.
- Author:
- Chakib Daii
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic PathReturns the directory path of the currently running JAR file.static PropertiesgetProperties(String filePath) Loads properties from a properties file at the given path, and resolves any placeholders within the property values.static PropertiesgetPropertiesFromResources(String filePath) Loads properties from a resource file located in the classpath.static InputStreamopenStream(URL url, boolean useCaches) Opens anInputStreamfrom the specified URL with an option to enable or disable caching.readFileLines(String filePath) Reads all lines from a text file and returns them as a list of strings.
-
Constructor Details
-
ResourceUtils
private ResourceUtils()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
readFileLines
Reads all lines from a text file and returns them as a list of strings. Uses UTF-8 encoding by default.- Parameters:
filePath- the path of the file to read- Returns:
- a list of lines read from the file
- Throws:
IOException- if an I/O error occurs reading from the file
-
getJarDirectory
Returns the directory path of the currently running JAR file. If running from a JAR file, returns the directory containing the JAR. If running from an IDE or file system, returns the directory path directly.- Returns:
- the directory path of the running JAR or execution directory
- Throws:
NaftahBugError- if unable to determine the JAR directory due to URI syntax issues
-
openStream
Opens anInputStreamfrom the specified URL with an option to enable or disable caching. Useful for preventing file descriptor leaks when reading from file system URLs.- Parameters:
url- the URL to open a connection touseCaches- whether to use caches or not- Returns:
- an input stream for reading from the URL connection
- Throws:
IOException- if an I/O exception occurs while opening the stream
-
getProperties
Loads properties from a properties file at the given path, and resolves any placeholders within the property values.- Parameters:
filePath- the path of the properties file to load- Returns:
- a
Propertiesobject containing the loaded properties - Throws:
NaftahBugError- if an error occurs while reading or loading the properties
-
getPropertiesFromResources
Loads properties from a resource file located in the classpath.The resource file must be accessible via the class loader. If the file cannot be found or read, a
NaftahBugErroris thrown. The error message is in Arabic for clarity when the file is missing.- Parameters:
filePath- The path to the resource file in the classpath.- Returns:
- A
Propertiesobject containing all properties from the file. - Throws:
NaftahBugError- If the file cannot be found or an I/O error occurs.
-