Class IOUtil


  • public final class IOUtil
    extends Object
    IO Utilities.
    • Method Detail

      • quietClose

        public static void quietClose​(Reader reader)
        Quietly closes the reader. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the reader and catch any IOException which may be thrown and ignore it.
        Parameters:
        reader - Reader to close
      • quietClose

        public static void quietClose​(InputStream stream)
        Quietly closes the stream. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the stream and catch any IOException which may be thrown.
        Parameters:
        stream - Stream to close
      • quietClose

        public static void quietClose​(Writer writer)
        Quietly closes the writer. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the Writer and catch any IOException which may be thrown.
        Parameters:
        writer - Writer to close
      • quietClose

        public static void quietClose​(OutputStream stream)
        Quietly closes the stream. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the stream and catch any IOException which may be thrown.
        Parameters:
        stream - Stream to close
      • quietClose

        public static void quietClose​(Statement stmt)
        Quietly closes the statement. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the statement and catch any SQLException which may be thrown.
        Parameters:
        stmt - Statement to close
        Since:
        1.3
      • quietClose

        public static void quietClose​(Connection conn)
        Quietly closes the connection. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the connection and catch any SQLException which may be thrown.
        Parameters:
        conn - Connection to close
        Since:
        1.3
      • quietClose

        public static void quietClose​(ResultSet resultset)
        Quietly closes the resultset. This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the connection and catch any SQLException which may be thrown.
        Parameters:
        resultset - ResultSet to close
        Since:
        1.3
      • getResourcePath

        public static String getResourcePath​(Class<?> c,
                                             String res)
        Get the path to a resource base on the package of given class.
        Parameters:
        c - Class to get the package path too.
        res - Name of the resource to get the path of.
        Returns:
        Returns the fully quilified path to a resource.
      • getResourceAsStream

        public static InputStream getResourceAsStream​(Class<?> clazz,
                                                      String res)
        Returns an input stream of the resource specified.
        Parameters:
        clazz -
        res -
        Returns:
        Returns an InputStream to the resource.
      • getResourceAsBytes

        public static byte[] getResourceAsBytes​(Class<?> clazz,
                                                String res)
        Get the resource as a byte array.
        Parameters:
        clazz -
        res -
        Returns:
      • getResourceAsString

        public static String getResourceAsString​(Class<?> clazz,
                                                 String res,
                                                 Charset charset)
        Read the entire stream into a String and return it.
        Parameters:
        clazz -
        res -
        charset -
        Returns:
      • getResourceAsString

        public static String getResourceAsString​(Class<?> clazz,
                                                 String res)
        Read the entire stream into a String and return it.
        Parameters:
        clazz -
        res -
        Returns:
      • inputStreamToBytes

        public static byte[] inputStreamToBytes​(InputStream ins)
        Takes a 'InputStream' and returns a byte array.
        Parameters:
        ins -
        Returns:
      • readerToString

        public static String readerToString​(Reader rdr)
        Takes a 'Reader' and returns the contents as a string.
        Parameters:
        rdr - Producer for the string data.
        Returns:
        Null if the 'Reader' is broken or empty otherwise the contents as a string.
      • copyFile

        public static boolean copyFile​(File src,
                                       File dest)
                                throws IOException
        Copies a file to a destination.
        Parameters:
        src - The source must be a file
        dest - This can be a directory or a file.
        Returns:
        True if succeeded otherwise false.
        Throws:
        IOException
      • copyFile

        public static long copyFile​(InputStream fis,
                                    OutputStream fos)
                             throws IOException
        Copies one file to another. NOTE: does not close streams.
        Parameters:
        fis -
        fos -
        Returns:
        total bytes copied.
        Throws:
        IOException
      • readInputStreamBytes

        public static byte[] readInputStreamBytes​(InputStream is,
                                                  boolean close)
                                           throws IOException
        Reads an entire file and returns the bytes.
        Parameters:
        is -
        close - if true, close when finished reading.
        Returns:
        file bytes.
        Throws:
        IOException
      • delete

        public static void delete​(File file)
                           throws IOException
        Recursively delete all the files in a directory and the directory.
        Parameters:
        file - the file to delete
        Throws:
        RuntimeException - if there is file that can not be deleted.
        IOException
      • loadPropertiesFile

        public static Properties loadPropertiesFile​(File file)
                                             throws IOException
        Loads the given file as a Properties file.
        Parameters:
        file - the file to load the propertied from
        Returns:
        properties loaded
        Throws:
        IOException
      • storePropertiesFile

        public static void storePropertiesFile​(File f,
                                               Properties properties)
                                        throws IOException
        Stores the given file as a Properties file.
        Throws:
        IOException
      • getResourceAsProperties

        public static Properties getResourceAsProperties​(ClassLoader loader,
                                                         String path)
                                                  throws IOException
        Loads the given resource as a properties object.
        Parameters:
        loader - The class loader
        path - The path to the resource
        Returns:
        The properties or null if not found
        Throws:
        IOException - If an error occurs reading it
      • extractResourceToFile

        public static void extractResourceToFile​(Class<?> clazz,
                                                 String path,
                                                 File file)
                                          throws IOException
        Extracts the resource to a file.
        Parameters:
        clazz - The class, relative to which path is resolved
        path - The path to the resource
        file - The file to extract to
        Throws:
        IOException - If an error occurs reading it
      • unjar

        public static void unjar​(JarFile jarFile,
                                 File toDir)
                          throws IOException
        Unjars the given file to the given directory. Does not close the JarFile when finished.
        Parameters:
        jarFile - The file to unjar.
        toDir - The directory to unjar to.
        Throws:
        IOException
      • readFileUTF8

        public static String readFileUTF8​(File file)
                                   throws IOException
        Reads the given file as UTF-8
        Parameters:
        file - The file to read
        Returns:
        The contents of the file
        Throws:
        IOException - if there is an issue reading the file.
      • readFileBytes

        public static byte[] readFileBytes​(File file)
                                    throws IOException
        Reads the given file as bytes
        Parameters:
        file - The file to read
        Returns:
        The contents of the file
        Throws:
        IOException - if there is an issue reading the file.
      • writeFileUTF8

        public static void writeFileUTF8​(File file,
                                         String contents)
                                  throws IOException
        Write the contents of the string out to a file in UTF-8 format.
        Parameters:
        file - the file to write to.
        contents - the contents of the file to write to.
        Throws:
        IOException - if there is an issue writing the file.
        NullPointerException - if the file parameter is null.
      • makeURL

        public static URL makeURL​(File dir,
                                  String path)
                           throws IOException
        Make a URL from a directory and path.
        Parameters:
        dir - directory to start from.
        path - file or path to create the url.
        Returns:
        URL based on the parameter provided.
        Throws:
        IOException - if the URL create from the parameters does not specify a file.
      • loadPropertiesFile

        public static Properties loadPropertiesFile​(String string)
                                             throws IOException
        Attempt to load file based on a string base filename.
        Parameters:
        string - represents the file.
        Returns:
        a loaded properties file.
        Throws:
        IOException - if there is an issue.