Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    Util class for handling file I/O.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void deleteFolderRecursively​(java.io.File dir)
      Helper method to delete non-empty folders, i.e., recursively deleting all contained files and sub-folders.
      static java.io.File getExistingFileWithHighestPriority​(java.lang.String resourcePath, java.lang.String... fileSystemPaths)
      Returns the file for a given path with the highest priority which also exists; the resource path is the backup solution with lowest priority.
      static java.util.List<java.io.File> getFilesOfFolder​(java.io.File folder)
      Returns a list of files contained in the specified folder.
      static boolean move​(java.io.File from, java.io.File to)
      This operation moves a file "from" to a destination "to".
      static boolean move​(java.lang.String from, java.lang.String to)
      Moves the path "from" to a destination path "to".
      static java.util.List<java.lang.String> readFileAsList​(java.io.File file)
      Reads the content of the given file as a list of strings.
      static java.util.List<java.lang.String> readFileAsList​(java.lang.String filename)
      Reads the content of the given file as a list of strings.
      static java.util.List<java.util.List<java.lang.String>> readFileAsMatrix​(java.lang.String filename, java.lang.String separation)
      Reads the content of the given file as a matrix of string which are separated by the given separation string.
      static java.lang.String readFileAsString​(java.io.File file)
      Reads the content of the given file into a single string.
      static java.lang.String readFileAsString​(java.lang.String filename)
      Reads the content of the given file into a single string.
      static java.util.Properties readPropertiesFile​(java.io.File propertiesFile)
      Reads the given file into a Properties object.
      static void requireFileExists​(java.io.File file)
      Checks whetehr a given file exists and if so, whether it is actually a file and not a directory.
      static void serializeObject​(java.lang.Object object, java.lang.String pathname)
      This method helps to serialize class objects as files.
      static void touch​(java.lang.String filename)
      Creates an empty file for the given path if it does not already exist.
      static java.lang.Object unserializeObject​(java.lang.String pathname)
      This method can be used to unserialize an object from disk (located at the specified path) and restore the original object.
      static void writeFileAsList​(java.util.List<java.lang.String> lines, java.lang.String filename)
      Writes List of strings as lines to given file
      static void zipFiles​(java.util.Collection<java.lang.String> files, java.lang.String archive)
      Archives a collection of files specified by their paths into a zip-file.
      • Methods inherited from class java.lang.Object

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

      • readFileAsList

        public static java.util.List<java.lang.String> readFileAsList​(java.io.File file)
                                                               throws java.io.IOException
        Reads the content of the given file as a list of strings.
        Parameters:
        file - The file to be read.
        Returns:
        The list of strings representing the content of the file.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • readFileAsList

        public static java.util.List<java.lang.String> readFileAsList​(java.lang.String filename)
                                                               throws java.io.IOException
        Reads the content of the given file as a list of strings.
        Parameters:
        filename - The path to the file to be read.
        Returns:
        The list of strings representing the content of the file.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • readFileAsString

        public static java.lang.String readFileAsString​(java.io.File file)
                                                 throws java.io.IOException
        Reads the content of the given file into a single string.
        Parameters:
        file - The file to be read.
        Returns:
        The String representing the content of the file.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • readFileAsString

        public static java.lang.String readFileAsString​(java.lang.String filename)
                                                 throws java.io.IOException
        Reads the content of the given file into a single string.
        Parameters:
        filename - The path to the file to be read.
        Returns:
        The String representing the content of the file.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • readFileAsMatrix

        public static java.util.List<java.util.List<java.lang.String>> readFileAsMatrix​(java.lang.String filename,
                                                                                        java.lang.String separation)
                                                                                 throws java.io.IOException
        Reads the content of the given file as a matrix of string which are separated by the given separation string.
        Parameters:
        filename - The path to the file to be read.
        separation - The string separating the matrix entries of a row.
        Returns:
        The matrix of strings.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • readPropertiesFile

        public static java.util.Properties readPropertiesFile​(java.io.File propertiesFile)
                                                       throws java.io.IOException
        Reads the given file into a Properties object.
        Parameters:
        propertiesFile - The file to be read.
        Returns:
        The properties object loaded from the specified file.
        Throws:
        java.io.IOException - Thrown, if there are issues reading the file.
      • zipFiles

        public static void zipFiles​(java.util.Collection<java.lang.String> files,
                                    java.lang.String archive)
                             throws java.io.IOException
        Archives a collection of files specified by their paths into a zip-file.
        Parameters:
        files - The files to be archived in a zip file.
        archive - The path to output the zipped files.
        Throws:
        java.io.IOException - Thrown, if an issue occurs while creating zip entries or writing the zip file to disk.
      • serializeObject

        public static void serializeObject​(java.lang.Object object,
                                           java.lang.String pathname)
                                    throws java.io.IOException
        This method helps to serialize class objects as files.
        Parameters:
        object - The object to be serialized.
        pathname - The path where to store the serialized object.
        Throws:
        java.io.IOException - Thrown if the object cannot be serialized or the serialized object cannot be written to disk.
      • unserializeObject

        public static java.lang.Object unserializeObject​(java.lang.String pathname)
                                                  throws java.io.IOException,
                                                         java.lang.ClassNotFoundException
        This method can be used to unserialize an object from disk (located at the specified path) and restore the original object.
        Parameters:
        pathname - The path from where to unserialize the object.
        Returns:
        The unserialized object.
        Throws:
        java.io.IOException - Thrown, if the binary file cannot be read.
        java.lang.ClassNotFoundException - Thrown, if the class of the object which is unserialized cannot be found on the classpath.
      • touch

        public static void touch​(java.lang.String filename)
        Creates an empty file for the given path if it does not already exist.
        Parameters:
        filename - The path of the file.
      • move

        public static boolean move​(java.io.File from,
                                   java.io.File to)
        This operation moves a file "from" to a destination "to".
        Parameters:
        from - The original file.
        to - The destination of the file.
        Returns:
        Returns true, iff the move operation was successful.
      • move

        public static boolean move​(java.lang.String from,
                                   java.lang.String to)
        Moves the path "from" to a destination path "to".
        Parameters:
        from - The original path.
        to - The destination path where to move the original path to.
        Returns:
        Returns true, iff the move operation was successful.
      • getFilesOfFolder

        public static java.util.List<java.io.File> getFilesOfFolder​(java.io.File folder)
        Returns a list of files contained in the specified folder.
        Parameters:
        folder - The folder for which the list of files shall be returned.
        Returns:
        A list of files contained in the given folder.
      • requireFileExists

        public static void requireFileExists​(java.io.File file)
                                      throws FileIsDirectoryException,
                                             java.io.FileNotFoundException
        Checks whetehr a given file exists and if so, whether it is actually a file and not a directory.
        Parameters:
        file - The file to be checked.
        Throws:
        FileIsDirectoryException - Is thrown if the file exists but is a directory.
        java.io.FileNotFoundException - Is thrown if there exists no such file.
      • getExistingFileWithHighestPriority

        public static java.io.File getExistingFileWithHighestPriority​(java.lang.String resourcePath,
                                                                      java.lang.String... fileSystemPaths)
        Returns the file for a given path with the highest priority which also exists; the resource path is the backup solution with lowest priority. The getter iterates over the fileSytemPaths array and returns the first existing file, i.e. paths with a lower array index have higher priority. If there is no element in the array or none of the given paths exists as a file, the file corresponding to the resource path will be returned as a fall-back, i.e., the resource path has the lowest priority.
        Parameters:
        resourcePath - The resource path that is to be taken as a fall-back.
        fileSystemPaths - An array of paths with descending priority.
        Returns:
        The existing file with highest priority.
      • deleteFolderRecursively

        public static void deleteFolderRecursively​(java.io.File dir)
                                            throws java.io.IOException
        Helper method to delete non-empty folders, i.e., recursively deleting all contained files and sub-folders.
        Parameters:
        dir - The folder to be deleted.
        Throws:
        java.io.IOException - Thrown, if there an issue arises while deleting all the files and sub-folders.
      • writeFileAsList

        public static void writeFileAsList​(java.util.List<java.lang.String> lines,
                                           java.lang.String filename)
                                    throws java.io.IOException
        Writes List of strings as lines to given file
        Parameters:
        lines -
        filename -
        Throws:
        java.io.IOException