Class ResourceUtils

java.lang.Object
org.daiitech.naftah.utils.ResourceUtils

public final class ResourceUtils extends Object
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 Details

    • ResourceUtils

      private ResourceUtils()
      Private constructor to prevent instantiation. Throws NaftahBugError if called.
  • Method Details

    • readFileLines

      public static List<String> readFileLines(String filePath) throws IOException
      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

      public static Path 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

      public static InputStream openStream(URL url, boolean useCaches) throws IOException
      Opens an InputStream from 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 to
      useCaches - 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

      public static Properties getProperties(String filePath)
      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 Properties object containing the loaded properties
      Throws:
      NaftahBugError - if an error occurs while reading or loading the properties
    • getPropertiesFromResources

      public static Properties getPropertiesFromResources(String filePath)
      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 NaftahBugError is 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 Properties object containing all properties from the file.
      Throws:
      NaftahBugError - If the file cannot be found or an I/O error occurs.