Class FileUtilities


  • public class FileUtilities
    extends Object
    This class is a collection of useful file-related static methods. Refer to the JavaDoc of the methods for more details.
    Author:
    faessler
    • Constructor Detail

      • FileUtilities

        public FileUtilities()
    • Method Detail

      • getReaderFromFile

        public static BufferedReader getReaderFromFile​(File file)
                                                throws IOException
        Returns a reader from the file file where the file may be a regular file or gzipped. The gzip format is recognized by the file extensions .gz or .gzip.
        Parameters:
        file - The file to read.
        Returns:
        A reader for file.
        Throws:
        IOException - If opening the file fails.
      • getWriterToFile

        public static BufferedWriter getWriterToFile​(File file,
                                                     boolean append)
                                              throws IOException
        Returns a writer to the file file where the destination file may will be gzipped if file has the extension .gz or .gzip.
        Parameters:
        file - The file to write.
        append - Whether to append to the file.
        Returns:
        A writer for file.
        Throws:
        IOException - If opening the file fails.
      • getWriterToFile

        public static BufferedWriter getWriterToFile​(File file)
                                              throws IOException
        Returns a writer to the file file where the destination file may will be gzipped if file has the extension .gz or .gzip.
        Parameters:
        file - The file to write.
        Returns:
        A writer for file.
        Throws:
        IOException - If opening the file fails.
      • createJarFile

        public static void createJarFile​(File outputFile,
                                         File... files)
                                  throws IOException
        Creates a JAR file with the given files as content.
        Parameters:
        outputFile - The destination where the JAR file should be written.
        files - The files to be included into the JAR file.
        Throws:
        IOException - If writing the JAR file fails.
      • addFileToJarOutputStream

        public static void addFileToJarOutputStream​(File source,
                                                    StringBuilder rootPath,
                                                    JarOutputStream target)
                                             throws IOException
        Adds a file entry to a JarOutputStream. Source: https://stackoverflow.com/questions/1281229/how-to-use-jaroutputstream-to-create-a-jar-file
        Parameters:
        source - The file that should be added into the JAR.
        rootPath -
        target - The JAR file to add source to.
        Throws:
        IOException - If adding the file fails.
      • findResource

        public static InputStream findResource​(String name)
                                        throws IOException
        Tries to find a resource by the given name. The name may be a path to a regular file, an URI or a classpath resource.
        Parameters:
        name - The resource name to find.
        Returns:
        The input stream from the found resource or null if the resource could not be found.
        Throws:
        IOException - If reading the resource fails.