Class ResourceFileUtils

java.lang.Object
org.saidone.utils.ResourceFileUtils

public class ResourceFileUtils extends Object
Utility class providing methods to access files from filesystem or classpath resources.

This class offers methods to obtain a File instance from a specified resource path. It first attempts to locate the file in the local filesystem using an absolute or relative path. If the file is not found in the filesystem, it will attempt to locate and extract the resource from the classpath. When accessing resources from the classpath, the method creates a temporary file which will be deleted on JVM exit, or will use a specified destination path if provided. Logging is used to notify where the file was found or if a resource is being loaded from the classpath.

  • Constructor Details

    • ResourceFileUtils

      public ResourceFileUtils()
  • Method Details

    • getFileFromResource

      public File getFileFromResource(String resourcePath) throws IOException
      Obtains a file from either the filesystem or classpath resources.
      Parameters:
      resourcePath - the path to the resource, either absolute path in the filesystem or a classpath resource
      Returns:
      the File object representing the resource
      Throws:
      IOException
    • getFileFromResource

      public File getFileFromResource(String resourcePath, String destinationPath) throws FileNotFoundException, IOException
      Retrieves a file from the given path, searching first in the filesystem and then in the classpath resources.
      If the specified resource path exists as a file in the filesystem, a File object pointing to this location is returned. If the file does not exist, the method attempts to locate the resource in the application's classpath. If found, it will extract the resource as a temporary file, or use a specific destination path if provided. If a temporary file is created, it will be marked for deletion on JVM exit.
      Parameters:
      resourcePath - the path to the resource in the filesystem or the name of the classpath resource
      destinationPath - optional path for the extracted resource file (used if resource is found in classpath); if null, a temporary file is created
      Returns:
      a File object representing the file or a copy of the extracted classpath resource
      Throws:
      FileNotFoundException - if the resource cannot be found in both the filesystem and classpath
      IOException - if an I/O error occurs during file access or resource extraction