- java.lang.Object
-
- org.tentackle.common.FileHelper
-
public class FileHelper extends java.lang.ObjectHelper methods for file handling.- Author:
- harald
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopy(java.io.File source, java.io.File dest)Copies a file.static java.io.InputStreamcreateInputStream(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.PropertiesloadProperties(java.lang.String name)Loads properties.
Tries to load from filesystem first, then from classpath.static java.util.PropertiesloadProperties(java.lang.String name, boolean asResource)Loads properties from a resource.static voidremoveDirectory(java.lang.String name)Removes a directory and all its contents recursively.static voidunzip(java.io.File zipFile, java.io.File destDir, java.util.function.Consumer<java.io.File> fileConsumer)Unzips a zip file.
-
-
-
Method Detail
-
createInputStream
public static java.io.InputStream createInputStream(java.lang.Class<?> caller, java.lang.String name) throws java.io.IOExceptionCreates an input stream from a resource.- Parameters:
caller- the caller class, null to determine via Stackwalkername- the resource name- Returns:
- the input stream
- Throws:
java.io.IOException- if reading the resources failedjava.io.FileNotFoundException- if no such resources found
-
loadProperties
public static java.util.Properties loadProperties(java.lang.String name, boolean asResource) throws java.io.IOExceptionLoads properties from a resource.- Parameters:
name- the properties name, null if return empty propertiesasResource- 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 failedjava.io.FileNotFoundException- if no properties found
-
loadProperties
public static java.util.Properties loadProperties(java.lang.String name) throws java.io.IOExceptionLoads 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 failedjava.io.FileNotFoundException- if no properties found
-
copy
public static void copy(java.io.File source, java.io.File dest) throws java.io.IOExceptionCopies a file.- Parameters:
source- the source filedest- the destination file- Throws:
java.io.IOException- if some I/O error
-
download
public static byte[] download(java.net.URL url) throws java.io.IOExceptionDownloads 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.IOExceptionRemoves 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.IOExceptionUnzips a zip file.- Parameters:
zipFile- the zip filedestDir- the destination directoryfileConsumer- optional consumer to receive the file being unzipped- Throws:
java.io.IOException- if failed
-
-