Class FileHelper


  • public class FileHelper
    extends Object
    Helper methods for file handling.
    Author:
    harald
    • Method Detail

      • createInputStream

        public static InputStream createInputStream​(Class<?> caller,
                                                    String name)
                                             throws IOException
        Creates an input stream from a resource.
        Parameters:
        caller - the caller class, null to determine via Stackwalker
        name - the resource name
        Returns:
        the input stream
        Throws:
        IOException - if reading the resources failed
        FileNotFoundException - if no such resources found
      • loadProperties

        public static Properties loadProperties​(String name,
                                                boolean asResource)
                                         throws IOException
        Loads properties from a resource.
        Parameters:
        name - the properties name, null if return empty properties
        asResource - true if load from the classpath, false if from filesystem
        Returns:
        the properties, empty if filename is null
        Throws:
        IOException - if reading the property file failed
        FileNotFoundException - if no properties found
      • loadProperties

        public static Properties loadProperties​(String name)
                                         throws IOException
        Loads properties.
        Tries to load from filesystem first, then from classpath.
        Parameters:
        name - the properties name, null if return empty properties
        Returns:
        the properties, empty if filename is null
        Throws:
        IOException - if reading the property file failed
        FileNotFoundException - if no properties found
      • copy

        public static void copy​(File source,
                                File dest)
                         throws IOException
        Copies a file.
        Parameters:
        source - the source file
        dest - the destination file
        Throws:
        IOException - if some I/O error
      • copyDirectory

        public static int copyDirectory​(File srcDir,
                                        File dstDir,
                                        boolean recursive,
                                        FileFilter filter)
                                 throws IOException
        Copies all files from one directory to another.
        Parameters:
        srcDir - the source directory
        dstDir - the destination directory
        recursive - true if copy also subdirectories
        filter - optional file filter, null if none
        Returns:
        number of files copied
        Throws:
        IOException - if copy failed
      • download

        public static byte[] download​(URL url)
                               throws IOException
        Downloads data from a URL into memory.
        Parameters:
        url - the URL
        Returns:
        the data as bytes
        Throws:
        IOException - if failed
      • removeDirectory

        public static void removeDirectory​(String name)
                                    throws IOException
        Removes a directory and all its contents recursively.
        Parameters:
        name - the directory name
        Throws:
        IOException - if failed
      • unzip

        public static void unzip​(File zipFile,
                                 File destDir,
                                 Consumer<File> fileConsumer)
                          throws IOException
        Unzips a zip file.
        Parameters:
        zipFile - the zip file
        destDir - the destination directory
        fileConsumer - optional consumer to receive the file being unzipped
        Throws:
        IOException - if failed