public final class FileUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
FileUtils.CollocatedTempFile
A collocated temporary file, that resides next to a "target" file, and is removed when closed.
|
static interface |
FileUtils.FileWriter
A file writer, that accepts a
Path to write some content to. |
static interface |
FileUtils.TempFile
A temporary file, that is removed when closed.
|
Modifier | Constructor and Description |
---|---|
private |
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static FileUtils.TempFile |
newTempFile()
Creates a
FileUtils.TempFile instance and backing temporary file on file system. |
static FileUtils.CollocatedTempFile |
newTempFile(java.nio.file.Path file)
Creates a
FileUtils.CollocatedTempFile instance for given file without backing file. |
static void |
writeFile(java.nio.file.Path target,
FileUtils.FileWriter writer)
Writes file without backup.
|
private static void |
writeFile(java.nio.file.Path target,
FileUtils.FileWriter writer,
boolean doBackup)
Utility method to write out file to disk in "atomic" manner, with optional backups (".bak") if needed.
|
static void |
writeFileWithBackup(java.nio.file.Path target,
FileUtils.FileWriter writer)
Writes file with backup copy (appends ".bak" extension).
|
public static FileUtils.TempFile newTempFile() throws java.io.IOException
FileUtils.TempFile
instance and backing temporary file on file system. It will be located in the default
temporary-file directory. Returned instance should be handled in try-with-resource construct and created
temp file is removed (if exists) when returned instance is closed.
This method uses Files.createTempFile(String, String, java.nio.file.attribute.FileAttribute[])
to create
the temporary file on file system.
java.io.IOException
public static FileUtils.CollocatedTempFile newTempFile(java.nio.file.Path file) throws java.io.IOException
FileUtils.CollocatedTempFile
instance for given file without backing file. The path will be located in
same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance
should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is
closed. The FileUtils.CollocatedTempFile.move()
makes possible to atomically replace passed in file with the
processed content written into a file backing the FileUtils.CollocatedTempFile
instance.
The file
nor it's parent directories have to exist. The parent directories are created if needed.
This method uses Path.resolve(String)
to create the temporary file path in passed in file parent
directory, but it does NOT create backing file on file system.
java.io.IOException
public static void writeFile(java.nio.file.Path target, FileUtils.FileWriter writer) throws java.io.IOException
target
- that is the target file (must be file, the path must have parent).writer
- the writer that will accept a Path
to write content to.java.io.IOException
- if at any step IO problem occurs.public static void writeFileWithBackup(java.nio.file.Path target, FileUtils.FileWriter writer) throws java.io.IOException
target
- that is the target file (must be file, the path must have parent).writer
- the writer that will accept a Path
to write content to.java.io.IOException
- if at any step IO problem occurs.private static void writeFile(java.nio.file.Path target, FileUtils.FileWriter writer, boolean doBackup) throws java.io.IOException
target
- that is the target file (must be an existing or non-existing file, the path must have parent).writer
- the writer that will accept a Path
to write content to.doBackup
- if true
, and target file is about to be overwritten, a ".bak" file with old contents will
be created/overwritten.java.io.IOException
- if at any step IO problem occurs.