Package org.eclipse.aether.util
Class FileUtils
java.lang.Object
org.eclipse.aether.util.FileUtils
A utility class to write files.
- Since:
- 1.9.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
A collocated temporary file, that resides next to a "target" file, and is removed when closed.static interface
A file writer, that accepts aPath
to write some content to.static interface
A temporary file, that is removed when closed. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
On Windows we use pre-NIO2 way to copy files, as for some reason it works.static FileUtils.TempFile
Creates aFileUtils.TempFile
instance and backing temporary file on file system.static FileUtils.CollocatedTempFile
newTempFile
(Path file) Creates aFileUtils.CollocatedTempFile
instance for given file without backing file.static void
writeFile
(Path target, FileUtils.FileWriter writer) Writes file without backup.private static void
writeFile
(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
(Path target, FileUtils.FileWriter writer) Writes file with backup copy (appends ".bak" extension).
-
Field Details
-
IS_WINDOWS
private static final boolean IS_WINDOWS
-
-
Constructor Details
-
FileUtils
private FileUtils()
-
-
Method Details
-
newTempFile
Creates aFileUtils.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.- Throws:
IOException
-
newTempFile
Creates aFileUtils.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. TheFileUtils.CollocatedTempFile.move()
makes possible to atomically replace passed in file with the processed content written into a file backing theFileUtils.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.- Throws:
IOException
-
copy
On Windows we use pre-NIO2 way to copy files, as for some reason it works. Beat me why.- Throws:
IOException
-
writeFile
Writes file without backup.- Parameters:
target
- that is the target file (must be file, the path must have parent).writer
- the writer that will accept aPath
to write content to.- Throws:
IOException
- if at any step IO problem occurs.
-
writeFileWithBackup
Writes file with backup copy (appends ".bak" extension).- Parameters:
target
- that is the target file (must be file, the path must have parent).writer
- the writer that will accept aPath
to write content to.- Throws:
IOException
- if at any step IO problem occurs.
-
writeFile
private static void writeFile(Path target, FileUtils.FileWriter writer, boolean doBackup) throws IOException Utility method to write out file to disk in "atomic" manner, with optional backups (".bak") if needed. This ensures that no other thread or process will be able to read not fully written files. Finally, this methos may create the needed parent directories, if the passed in target parents does not exist.- Parameters:
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 aPath
to write content to.doBackup
- iftrue
, and target file is about to be overwritten, a ".bak" file with old contents will be created/overwritten.- Throws:
IOException
- if at any step IO problem occurs.
-