Class FileResolver


  • public class FileResolver
    extends Object
    Utility class for resolving and reading configuration files from both filesystem and classpath (including JAR resources).

    This class provides a centralized way to handle file loading that works consistently across different deployment scenarios:

    • Files on the filesystem
    • Files in the classpath (e.g., src/main/resources)
    • Files inside JAR files
    Since:
    1.0.3
    Author:
    Bharat Kumar Malviya, GitHub: github.com/imBharatMalviya
    • Constructor Detail

      • FileResolver

        public FileResolver()
    • Method Detail

      • resolve

        public static FileResolver.ResolvedFile resolve​(String filePath)
        Resolves a file path by checking filesystem first, then classpath. For JAR resources, creates a temporary file that will be deleted on JVM exit.
        Parameters:
        filePath - the file path to resolve
        Returns:
        a ResolvedFile containing the resolved path and metadata, or null if not found
      • openInputStream

        public static InputStream openInputStream​(String filePath)
                                           throws IOException
        Opens an InputStream for reading a file from either filesystem or classpath.
        Parameters:
        filePath - the file path to read
        Returns:
        an InputStream for reading the file
        Throws:
        IOException - if the file cannot be opened or doesn't exist
      • openReader

        public static Reader openReader​(String filePath)
                                 throws IOException
        Opens a Reader for reading a file from either filesystem or classpath.
        Parameters:
        filePath - the file path to read
        Returns:
        a Reader for reading the file
        Throws:
        IOException - if the file cannot be opened or doesn't exist
      • exists

        public static boolean exists​(String filePath)
        Checks if a file exists in either filesystem or classpath.
        Parameters:
        filePath - the file path to check
        Returns:
        true if the file exists
      • getFileName

        public static String getFileName​(String filePath)
        Gets the filename (without path) from a file path.
        Parameters:
        filePath - the file path
        Returns:
        the filename, or null if the path is invalid