Class FileHelper


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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copy​(java.io.File source, java.io.File dest)
      Copies a file.
      static java.io.InputStream createInputStream​(java.lang.Class<?> caller, java.lang.String name)
      Creates an input stream from a resource.
      static byte[] download​(java.net.URL url)
      Downloads data from a URL into memory.
      static java.util.Properties loadProperties​(java.lang.String name)
      Loads properties.
      Tries to load from filesystem first, then from classpath.
      static java.util.Properties loadProperties​(java.lang.String name, boolean asResource)
      Loads properties from a resource.
      static void removeDirectory​(java.lang.String name)
      Removes a directory and all its contents recursively.
      static void unzip​(java.io.File zipFile, java.io.File destDir, java.util.function.Consumer<java.io.File> fileConsumer)
      Unzips a zip file.
      • Methods inherited from class java.lang.Object

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

      • createInputStream

        public static java.io.InputStream createInputStream​(java.lang.Class<?> caller,
                                                            java.lang.String name)
                                                     throws java.io.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:
        java.io.IOException - if reading the resources failed
        java.io.FileNotFoundException - if no such resources found
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.String name,
                                                          boolean asResource)
                                                   throws java.io.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:
        java.io.IOException - if reading the property file failed
        java.io.FileNotFoundException - if no properties found
      • loadProperties

        public static java.util.Properties loadProperties​(java.lang.String name)
                                                   throws java.io.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:
        java.io.IOException - if reading the property file failed
        java.io.FileNotFoundException - if no properties found
      • copy

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

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

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

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