- java.lang.Object
-
- org.tentackle.common.FileHelper
-
public class FileHelper extends Object
Helper methods for file handling.- Author:
- harald
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcopy(File source, File dest)Copies a file.static intcopyDirectory(File srcDir, File dstDir, boolean recursive, FileFilter filter)Copies all files from one directory to another.static InputStreamcreateInputStream(Class<?> caller, String name)Creates an input stream from a resource.static byte[]download(URL url)Downloads data from a URL into memory.static PropertiesloadProperties(String name)Loads properties.
Tries to load from filesystem first, then from classpath.static PropertiesloadProperties(String name, boolean asResource)Loads properties from a resource.static voidremoveDirectory(String name)Removes a directory and all its contents recursively.static voidunzip(File zipFile, File destDir, Consumer<File> fileConsumer)Unzips a zip file.
-
-
-
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 Stackwalkername- the resource name- Returns:
- the input stream
- Throws:
IOException- if reading the resources failedFileNotFoundException- 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 propertiesasResource- 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 failedFileNotFoundException- 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 failedFileNotFoundException- if no properties found
-
copy
public static void copy(File source, File dest) throws IOException
Copies a file.- Parameters:
source- the source filedest- 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 directorydstDir- the destination directoryrecursive- true if copy also subdirectoriesfilter- 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 filedestDir- the destination directoryfileConsumer- optional consumer to receive the file being unzipped- Throws:
IOException- if failed
-
-