Class FileFinder


  • public class FileFinder
    extends java.lang.Object

    Helper class for locating files. Read and write to them without worrying about the precise file path as long as the file is in the classpath. If the full path is specified, then it will be used regardless of whether the file is in the classpath or not.

    Note: the ClassLoader used to locate the resource is the same one used to load the FileFinder class, which is not necessarily the system ClassLoader (e.g. servlet engines often have custom ClassLoaders).

    Since:
    MicroStrategy Web 7.3.1 or earlier
    • Constructor Summary

      Constructors 
      Constructor Description
      FileFinder()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void check​(java.lang.String filePath)
      static void check​(java.lang.String filePath, int flags)
      Validates file attributes.
      static boolean checkIfValid​(java.lang.String filePath, int flags)
      Same as check(String, int), except this method returns true or false as to whether the validation succeeds instead of throwing exceptions.
      static java.lang.String findDir​(java.lang.String path)
      Locates a resource either from the filepath, or classpath, and returns its encapsulating directory.
      static java.lang.String findDir​(java.lang.String path, java.lang.ClassLoader cl)
      Locates a resource either from the filepath, or classpath, and returns its encapsulating directory.
      static java.lang.String findPath​(java.lang.String path)
      Locates a resource either from the filepath, or classpath.
      static java.lang.String findPath​(java.lang.String path, java.lang.ClassLoader cl)
      Locates a resource either from the filepath, or classpath.
      static java.lang.String findPathJar​(java.lang.String path)
      Locates a resource either from the filepath, or classpath.
      static java.lang.String findPathJar​(java.lang.String path, java.lang.ClassLoader cl)
      Locates a resource either from the filepath, or classpath.
      static java.net.URL findResource​(java.lang.String name)
      Finds resource with given name.
      static java.net.URL findResource​(java.lang.String name, java.lang.ClassLoader cl)
      Finds resource with given name.
      static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath)
      Attempts to locate resource from the given file path.
      static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath, java.lang.ClassLoader cl)
      Attempts to locate resource from the given file path.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileFinder

        public FileFinder()
    • Method Detail

      • findResource

        public static java.net.URL findResource​(java.lang.String name)
        Finds resource with given name.
        Parameters:
        name - resource name.
        Returns:
        URL of resource
        See Also:
        ClassLoader.getResource(String)
      • findResource

        public static java.net.URL findResource​(java.lang.String name,
                                                java.lang.ClassLoader cl)
        Finds resource with given name.
        Parameters:
        name - resource name.
        cl - The ClassLoader to use to find objects in the class path.
        Returns:
        URL of resource
        Since:
        MicroStrategy Web 9.0.0
        See Also:
        ClassLoader.getResource(String)
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath)
                                                       throws java.io.FileNotFoundException,
                                                              java.io.IOException
        Attempts to locate resource from the given file path. If not found, it will try using the application ClassLoader to locate the resource.
        Parameters:
        resourcePath - File path for the resource.
        Returns:
        The InputStream of resource file.
        Throws:
        java.io.FileNotFoundException - Thrown when resource file can not be found.
        java.io.IOException - Thrown if a problem occurs while deriving the path URL.
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resourcePath,
                                                              java.lang.ClassLoader cl)
                                                       throws java.io.FileNotFoundException,
                                                              java.io.IOException
        Attempts to locate resource from the given file path. If not found, it will try using the application ClassLoader to locate the resource.
        Parameters:
        resourcePath - File path for the resource.
        cl - The ClassLoader to use to find objects in the class path.
        Returns:
        The InputStream of resource file.
        Throws:
        java.io.FileNotFoundException - Thrown when resource file can not be found.
        java.io.IOException - Thrown if a problem occurs while deriving the path URL.
        Since:
        MicroStrategy Web 9.0.0
      • findPath

        public static java.lang.String findPath​(java.lang.String path)
                                         throws java.lang.IllegalArgumentException,
                                                java.io.FileNotFoundException,
                                                java.io.IOException
        Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will NOT return files located in a JAR. An IllegalArgumentException is thrown if the path is empty. This method is useful in obtaining paths in the file system. To find paths that may be inside a JAR, see findPathJar(String).
        Parameters:
        path - file name or file path - it should either be in the classpath, or be specified by a fully qualified path.
        Returns:
        URL path of the file.
        Throws:
        java.lang.IllegalArgumentException - if path is empty
        java.io.FileNotFoundException - if the file does not exist
        java.io.IOException - if a problem occurs while deriving the path URL
      • findPath

        public static java.lang.String findPath​(java.lang.String path,
                                                java.lang.ClassLoader cl)
                                         throws java.lang.IllegalArgumentException,
                                                java.io.FileNotFoundException,
                                                java.io.IOException
        Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will NOT return files located in a JAR. An IllegalArgumentException is thrown if the path is empty. This method is useful in obtaining paths in the file system. To find paths that may be inside a JAR, see findPathJar(String).
        Parameters:
        path - file name or file path - it should either be in the classpath, or be specified by a fully qualified path.
        cl - The ClassLoader to use to find objects in the class path.
        Returns:
        URL path of the file.
        Throws:
        java.lang.IllegalArgumentException - if path is empty
        java.io.FileNotFoundException - if the file does not exist
        java.io.IOException - if a problem occurs while deriving the path URL
        Since:
        MicroStrategy Web 9.0.0
      • findPathJar

        public static java.lang.String findPathJar​(java.lang.String path)
                                            throws java.lang.IllegalArgumentException,
                                                   java.io.FileNotFoundException,
                                                   java.io.IOException
        Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will return paths located in JARs on the classpath. To find paths excluding those in a JAR, see findPath(String).
        Parameters:
        path - either a fully qualified file path, or a relative resource path
        Returns:
        URL path of the file
        Throws:
        java.lang.IllegalArgumentException - if path is empty
        java.io.FileNotFoundException - if path does not exist
        java.io.IOException - if a problem occurs while deriving the path URL
      • findPathJar

        public static java.lang.String findPathJar​(java.lang.String path,
                                                   java.lang.ClassLoader cl)
                                            throws java.lang.IllegalArgumentException,
                                                   java.io.FileNotFoundException,
                                                   java.io.IOException
        Locates a resource either from the filepath, or classpath. A FileNotFoundException is thrown if it cannot be found. Note that this method will return paths located in JARs on the classpath. To find paths excluding those in a JAR, see findPath(String).
        Parameters:
        path - either a fully qualified file path, or a relative resource path
        cl - The ClassLoader to use to find objects in the class path.
        Returns:
        URL path of the file
        Throws:
        java.lang.IllegalArgumentException - if path is empty
        java.io.FileNotFoundException - if path does not exist
        java.io.IOException - if a problem occurs while deriving the path URL
        Since:
        MicroStrategy Web 9.0.0
      • findDir

        public static java.lang.String findDir​(java.lang.String path)
                                        throws java.io.FileNotFoundException,
                                               java.io.IOException
        Locates a resource either from the filepath, or classpath, and returns its encapsulating directory. Note that this method will NOT return files inside a JAR.
        Parameters:
        path - resource file name or resource file path - it should either be in the classpath, or be specified by a fully qualified path.
        Returns:
        The canonical path of the encapsulating directory of the resource file.
        Throws:
        java.io.FileNotFoundException - if the path does not exist
        java.io.IOException - if parent directory cannot be obtained from given path.
      • findDir

        public static java.lang.String findDir​(java.lang.String path,
                                               java.lang.ClassLoader cl)
                                        throws java.io.FileNotFoundException,
                                               java.io.IOException
        Locates a resource either from the filepath, or classpath, and returns its encapsulating directory. Note that this method will NOT return files inside a JAR.
        Parameters:
        path - resource file name or resource file path - it should either be in the classpath, or be specified by a fully qualified path.
        cl - The ClassLoader to use to find objects in the class path.
        Returns:
        The canonical path of the encapsulating directory of the resource file.
        Throws:
        java.io.FileNotFoundException - if the path does not exist
        java.io.IOException - if parent directory cannot be obtained from given path.
        Since:
        MicroStrategy Web 9.0.0
      • check

        public static void check​(java.lang.String filePath)
                          throws java.io.FileNotFoundException,
                                 java.io.IOException
        Parameters:
        filePath - file path
        Throws:
        java.io.FileNotFoundException - if filePath does not exist
        java.io.IOException - if check based on given flags fail
      • check

        public static void check​(java.lang.String filePath,
                                 int flags)
                          throws java.io.FileNotFoundException,
                                 java.io.IOException
        Validates file attributes. This operation will only work for file paths in the file system. Available flags: FLAG_FILE, FLAG_READABLE, FLAG_WRITABLE.
        Parameters:
        filePath - file path
        flags - set to check for read, write or file/directory validation
        Throws:
        java.io.FileNotFoundException - if filePath does not exist
        java.io.IOException - if check based on given flags fail
      • checkIfValid

        public static boolean checkIfValid​(java.lang.String filePath,
                                           int flags)
        Same as check(String, int), except this method returns true or false as to whether the validation succeeds instead of throwing exceptions.
        Parameters:
        filePath - The file path.
        flags - Set to check for read, write or file/directory validation.
        Returns:
        whether validation succeeds or fails
        Since:
        MicroStrategy Web 8.0.0