public class FileUtils
extends java.lang.Object
Set of methods to help and ease the manipulation of Files. Most likely adding minor behavior on top of existing Files methods.
| Constructor and Description |
|---|
FileUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
copy(java.nio.file.Path sourceFile,
java.nio.file.Path destinationFile)
Copies the content of a file found at a specified Path to another file located at another specified Path.
|
static void |
copyToFolder(java.nio.file.Path sourceFile,
java.nio.file.Path destinationFolder)
Copies the content of a file found at a specified Path to another file located in the specified destinationFolder.
|
static void |
createFileIfNeeded(java.nio.file.Path path)
Creates an empty approval file if it doesn’t exist yet.
|
static java.util.stream.Stream<java.nio.file.Path> |
listFiles(java.nio.file.Path baseDirectory)
Returns a list of all regular files found in a base directory up to a depth of MAX_DEPTH.
|
static java.lang.String |
silentRead(java.nio.file.Path file)
Reads the file located at the specified Path, and returns its content in case the file exists.
|
static void |
silentRecursiveRemove(java.io.File file)
Removes recursively all elements if
file is a directory, then removes file. |
static void |
silentRecursiveRemove(java.nio.file.Path path)
Removes recursively all elements if
path is a directory, then removes path. |
static void |
silentRemove(java.nio.file.Path file)
Removes the file located at the specified Path if it exists.
|
static void |
write(java.lang.String content,
java.nio.file.Path file)
Writes the specified
content in the file located at the specified Path. |
public static java.lang.String silentRead(java.nio.file.Path file)
Reads the file located at the specified Path, and returns its content in case the file exists.
If it doesn’t exist or if it cannot be read, that method will return an empty String and ignore the IOException. An error message will be written in System.err though.
file - The path of the file to be readpublic static void createFileIfNeeded(java.nio.file.Path path)
Creates an empty approval file if it doesn’t exist yet.
If it already exist, that method will do nothing. If there’s any issue while creating the approval file, the IOException will be wrapped in a RuntimeException and thrown.
public static void silentRemove(java.nio.file.Path file)
Removes the file located at the specified Path if it exists.
If the file doesn’t exist, nothing will be done.
If the file exists but cannot be removed, an error will simply be written in System.err and the Exception won’t be propagated (since it doesn’t block the framework from working properly). We strongly advise to have a look at the problem though.
file - The path of the file to be removedpublic static void silentRecursiveRemove(java.nio.file.Path path)
Removes recursively all elements if path is a directory, then removes path.
If the path doesn’t exist, nothing will be done.
path - the Path to be removedpublic static void silentRecursiveRemove(java.io.File file)
Removes recursively all elements if file is a directory, then removes file.
If the file doesn’t exist, nothing will be done.
file - the File to be removedpublic static void write(java.lang.String content,
java.nio.file.Path file)
Writes the specified content in the file located at the specified Path.
If the directories supposed to contain the specified file don’t exist, they’ll be created by this method automatically. A message will be written in System.err and a RuntimeException will be thrown in case of any error.
The provided content will then be written in the provided file. A RuntimeException will be thrown in case anything is going wrong will writing the content in the file.
content - The content to be written in the specified filefile - The file in which the content should be writtenpublic static void copy(java.nio.file.Path sourceFile,
java.nio.file.Path destinationFile)
Copies the content of a file found at a specified Path to another file located at another specified Path.
It’ll swallow all errors while reading the sourceFile (using an empty String as data) and only produce exceptions in case of errors while writing the new file.
sourceFile - The file from which the data to be copied should be readdestinationFile - The file to which the data to be copied should be writtenpublic static void copyToFolder(java.nio.file.Path sourceFile,
java.nio.file.Path destinationFolder)
Copies the content of a file found at a specified Path to another file located in the specified destinationFolder.
If the file doesn’t exist in the specified destinationFolder, it’ll be created. It’ll swallow all errors while reading the sourceFile file and only produce exceptions in case of errors while writing the new file.
sourceFile - The path from which the sourceFile file should be read (data to be copied)destinationFolder - The path in which the copy of the file should be made (parent destinationFolder of the copy then)public static java.util.stream.Stream<java.nio.file.Path> listFiles(java.nio.file.Path baseDirectory)
Returns a list of all regular files found in a base directory up to a depth of MAX_DEPTH.
If the provided baseDirectory isn’t actually a directory, it’ll return an empty stream instead.
baseDirectory - The base directory in which the regular files should be searched forCopyright © 2018 Write Them First!. All rights reserved.