public class FileUtil
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static void |
deleteFolderRecursively(java.io.File dir)
Helper method to delete non-empty folders, i.e., recursively deleting all contained files and sub-folders.
|
static java.io.File |
getExistingFileWithHighestPriority(java.lang.String resourcePath,
java.lang.String... fileSystemPaths)
Returns the file for a given path with the highest priority which also exists; the resource path is the backup solution with lowest priority.
|
static java.util.List<java.io.File> |
getFilesOfFolder(java.io.File folder)
Returns a list of files contained in the specified folder.
|
static boolean |
move(java.io.File from,
java.io.File to)
This operation moves a file "from" to a destination "to".
|
static boolean |
move(java.lang.String from,
java.lang.String to)
Moves the path "from" to a destination path "to".
|
static java.util.List<java.lang.String> |
readFileAsList(java.io.File file)
Reads the content of the given file as a list of strings.
|
static java.util.List<java.lang.String> |
readFileAsList(java.lang.String filename)
Reads the content of the given file as a list of strings.
|
static java.util.List<java.util.List<java.lang.String>> |
readFileAsMatrix(java.lang.String filename,
java.lang.String separation)
Reads the content of the given file as a matrix of string which are separated by the given separation string.
|
static java.lang.String |
readFileAsString(java.io.File file)
Reads the content of the given file into a single string.
|
static java.lang.String |
readFileAsString(java.lang.String filename)
Reads the content of the given file into a single string.
|
static java.util.Properties |
readPropertiesFile(java.io.File propertiesFile)
Reads the given file into a Properties object.
|
static void |
requireFileExists(java.io.File file)
Checks whetehr a given file exists and if so, whether it is actually a file and not a directory.
|
static void |
serializeObject(java.lang.Object object,
java.lang.String pathname)
This method helps to serialize class objects as files.
|
static void |
touch(java.lang.String filename)
Creates an empty file for the given path if it does not already exist.
|
static java.lang.Object |
unserializeObject(java.lang.String pathname)
This method can be used to unserialize an object from disk (located at the specified path) and restore the original object.
|
static void |
zipFiles(java.util.Collection<java.lang.String> files,
java.lang.String archive)
Archives a collection of files specified by their paths into a zip-file.
|
public static java.util.List<java.lang.String> readFileAsList(java.io.File file)
throws java.io.IOException
file - The file to be read.java.io.IOException - Thrown, if there are issues reading the file.public static java.util.List<java.lang.String> readFileAsList(java.lang.String filename)
throws java.io.IOException
filename - The path to the file to be read.java.io.IOException - Thrown, if there are issues reading the file.public static java.lang.String readFileAsString(java.io.File file)
throws java.io.IOException
file - The file to be read.java.io.IOException - Thrown, if there are issues reading the file.public static java.lang.String readFileAsString(java.lang.String filename)
throws java.io.IOException
filename - The path to the file to be read.java.io.IOException - Thrown, if there are issues reading the file.public static java.util.List<java.util.List<java.lang.String>> readFileAsMatrix(java.lang.String filename,
java.lang.String separation)
throws java.io.IOException
filename - The path to the file to be read.separation - The string separating the matrix entries of a row.java.io.IOException - Thrown, if there are issues reading the file.public static java.util.Properties readPropertiesFile(java.io.File propertiesFile)
throws java.io.IOException
propertiesFile - The file to be read.java.io.IOException - Thrown, if there are issues reading the file.public static void zipFiles(java.util.Collection<java.lang.String> files,
java.lang.String archive)
throws java.io.IOException
files - The files to be archived in a zip file.archive - The path to output the zipped files.java.io.IOException - Thrown, if an issue occurs while creating zip entries or writing the zip file to disk.public static void serializeObject(java.lang.Object object,
java.lang.String pathname)
throws java.io.IOException
object - The object to be serialized.pathname - The path where to store the serialized object.java.io.IOException - Thrown if the object cannot be serialized or the serialized object cannot be written to disk.public static java.lang.Object unserializeObject(java.lang.String pathname)
throws java.io.IOException,
java.lang.ClassNotFoundException
pathname - The path from where to unserialize the object.java.io.IOException - Thrown, if the binary file cannot be read.java.lang.ClassNotFoundException - Thrown, if the class of the object which is unserialized cannot be found on the classpath.public static void touch(java.lang.String filename)
filename - The path of the file.public static boolean move(java.io.File from,
java.io.File to)
from - The original file.to - The destination of the file.public static boolean move(java.lang.String from,
java.lang.String to)
from - The original path.to - The destination path where to move the original path to.public static java.util.List<java.io.File> getFilesOfFolder(java.io.File folder)
folder - The folder for which the list of files shall be returned.public static void requireFileExists(java.io.File file)
throws FileIsDirectoryException,
java.io.FileNotFoundException
file - The file to be checked.FileIsDirectoryException - Is thrown if the file exists but is a directory.java.io.FileNotFoundException - Is thrown if there exists no such file.public static java.io.File getExistingFileWithHighestPriority(java.lang.String resourcePath,
java.lang.String... fileSystemPaths)
resourcePath - The resource path that is to be taken as a fall-back.fileSystemPaths - An array of paths with descending priority.public static void deleteFolderRecursively(java.io.File dir)
throws java.io.IOException
dir - The folder to be deleted.java.io.IOException - Thrown, if there an issue arises while deleting all the files and sub-folders.