Package org.saidone.utils
Class ResourceFileUtils
java.lang.Object
org.saidone.utils.ResourceFileUtils
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetFileFromResource
(String resourcePath) Obtains a file from either the filesystem or classpath resources.getFileFromResource
(String resourcePath, String destinationPath) Retrieves a file from the given path, searching first in the filesystem and then in the classpath resources.
-
Constructor Details
-
ResourceFileUtils
public ResourceFileUtils()
-
-
Method Details
-
getFileFromResource
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, aFile
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 resourcedestinationPath
- 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 classpathIOException
- if an I/O error occurs during file access or resource extraction
-