Class ResourceUtils


  • public class ResourceUtils
    extends java.lang.Object
    A class to simplify access to ResourceUtils through the classloader.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.net.URL extractJarFileURL​(java.net.URL jarUrl)  
      static java.io.File getFile​(java.lang.String resourceLocation, java.lang.ClassLoader classLoader)  
      static java.io.File getFile​(java.net.URI resourceUri)  
      static java.io.File getFile​(java.net.URI resourceUri, java.lang.String description)  
      static java.io.File getFile​(java.net.URL resourceUrl)  
      static java.io.File getFile​(java.net.URL resourceUrl, java.lang.String description)  
      static java.io.Reader getReader​(java.io.File file, java.lang.String encoding)
      Returns a Reader for reading the specified file.
      static java.io.Reader getReader​(java.net.URL url, java.lang.String encoding)
      Returns a Reader for reading the specified url.
      static java.net.URL getResource​(java.lang.String resource)
      Returns the URL of the resource on the classpath.
      static java.net.URL getResource​(java.lang.String resource, java.lang.ClassLoader classLoader)
      Returns the URL of the resource on the classpath.
      static java.io.File getResourceAsFile​(java.lang.String resource)  
      static java.io.File getResourceAsFile​(java.lang.String resource, java.lang.ClassLoader classLoader)  
      static java.io.InputStream getResourceAsStream​(java.lang.String resource)
      Returns a resource on the classpath as a Stream object.
      static java.io.InputStream getResourceAsStream​(java.lang.String resource, java.lang.ClassLoader classLoader)
      Returns a resource on the classpath as a Stream object.
      static java.net.URL getURL​(java.lang.String resourceLocation, java.lang.ClassLoader classLoader)  
      static boolean isJarSimilarURL​(java.net.URL url)  
      static boolean isJarURL​(java.net.URL url)  
      static boolean isUrl​(java.lang.String resourceLocation)  
      static java.lang.String read​(java.io.File file, java.lang.String encoding)
      Returns a string from the specified file.
      static java.lang.String read​(java.io.Reader reader)
      Returns a string from the specified Reader object.
      static java.lang.String read​(java.net.URL url, java.lang.String encoding)
      Returns a string from the specified url.
      static java.net.URI toURI​(java.lang.String location)  
      static java.net.URI toURI​(java.net.URL url)  
      • Methods inherited from class java.lang.Object

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

      • ResourceUtils

        public ResourceUtils()
    • Method Detail

      • isUrl

        public static boolean isUrl​(java.lang.String resourceLocation)
      • getURL

        public static java.net.URL getURL​(java.lang.String resourceLocation,
                                          java.lang.ClassLoader classLoader)
                                   throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • getFile

        public static java.io.File getFile​(java.lang.String resourceLocation,
                                           java.lang.ClassLoader classLoader)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • getFile

        public static java.io.File getFile​(java.net.URL resourceUrl)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • getFile

        public static java.io.File getFile​(java.net.URL resourceUrl,
                                           java.lang.String description)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • getFile

        public static java.io.File getFile​(java.net.URI resourceUri)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • getFile

        public static java.io.File getFile​(java.net.URI resourceUri,
                                           java.lang.String description)
                                    throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • isJarURL

        public static boolean isJarURL​(java.net.URL url)
      • isJarSimilarURL

        public static boolean isJarSimilarURL​(java.net.URL url)
      • extractJarFileURL

        public static java.net.URL extractJarFileURL​(java.net.URL jarUrl)
                                              throws java.net.MalformedURLException
        Throws:
        java.net.MalformedURLException
      • toURI

        public static java.net.URI toURI​(java.net.URL url)
                                  throws java.net.URISyntaxException
        Throws:
        java.net.URISyntaxException
      • toURI

        public static java.net.URI toURI​(java.lang.String location)
                                  throws java.net.URISyntaxException
        Throws:
        java.net.URISyntaxException
      • getResource

        public static java.net.URL getResource​(java.lang.String resource)
                                        throws java.io.IOException
        Returns the URL of the resource on the classpath.
        Parameters:
        resource - the resource to find
        Returns:
        URL object for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResource

        public static java.net.URL getResource​(java.lang.String resource,
                                               java.lang.ClassLoader classLoader)
                                        throws java.io.IOException
        Returns the URL of the resource on the classpath.
        Parameters:
        classLoader - the class loader used to load the resource
        resource - the resource to find
        Returns:
        URL object for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResourceAsFile

        public static java.io.File getResourceAsFile​(java.lang.String resource)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getResourceAsFile

        public static java.io.File getResourceAsFile​(java.lang.String resource,
                                                     java.lang.ClassLoader classLoader)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resource)
                                                       throws java.io.IOException
        Returns a resource on the classpath as a Stream object.
        Parameters:
        resource - the resource to find
        Returns:
        an input stream for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getResourceAsStream

        public static java.io.InputStream getResourceAsStream​(java.lang.String resource,
                                                              java.lang.ClassLoader classLoader)
                                                       throws java.io.IOException
        Returns a resource on the classpath as a Stream object.
        Parameters:
        resource - the resource to find
        classLoader - the class loader
        Returns:
        an input stream for reading the resource; null if the resource could not be found
        Throws:
        java.io.IOException - if the resource cannot be found or read
      • getReader

        public static java.io.Reader getReader​(java.io.File file,
                                               java.lang.String encoding)
                                        throws java.io.IOException
        Returns a Reader for reading the specified file.
        Parameters:
        file - the file
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • getReader

        public static java.io.Reader getReader​(java.net.URL url,
                                               java.lang.String encoding)
                                        throws java.io.IOException
        Returns a Reader for reading the specified url.
        Parameters:
        url - the url
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.io.File file,
                                            java.lang.String encoding)
                                     throws java.io.IOException
        Returns a string from the specified file.
        Parameters:
        file - the file
        encoding - the encoding
        Returns:
        the reader instance
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.net.URL url,
                                            java.lang.String encoding)
                                     throws java.io.IOException
        Returns a string from the specified url.
        Parameters:
        url - the url
        encoding - the encoding
        Returns:
        the string
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations
      • read

        public static java.lang.String read​(java.io.Reader reader)
                                     throws java.io.IOException
        Returns a string from the specified Reader object.
        Parameters:
        reader - the reader
        Returns:
        the string
        Throws:
        java.io.IOException - if an error occurred when reading resources using any I/O operations