public class StagedWrite extends Object implements Closeable
StagedWrite stage = StagedWrite.begin(outputFile);
try {
OutputStream stream = stage.openOutputStream();
// write to stream
stream.close();
stage.commit();
} finally {
stage.close();
}
The logic used to implement this class is subject to race conditions, so it should not be used when multiple threads or processes may attempt to write the same file. In LensKit 3.0, the race condition will be removed on systems exposing POSIX file system semantics, but the APIs needed for us to provide that capability are not present on Java 6.
Modifier and Type | Method and Description |
---|---|
static StagedWrite |
begin(File target)
Begin a staged file writing operation.
|
void |
close()
Clean up the staged write, deleting the staging file if it still exists.
|
void |
commit()
Complete the staging write by replacing the target file with the staging file.
|
File |
getStagingFile()
Get the working file for this staging file.
|
File |
getTargetFile()
Get the target file for this staging file.
|
FileOutputStream |
openOutputStream()
Open an output stream for the staging file.
|
public static StagedWrite begin(File target)
target
- The file to write.public File getTargetFile()
public File getStagingFile()
public FileOutputStream openOutputStream() throws IOException
commit()
.IOException
- if there is an error opening the output stream.public void commit() throws IOException
IOException
- if there is an error moving the file.public void close()
commit()
. Typical
use of a staged write will call this method in a finally
block.close
in interface Closeable
close
in interface AutoCloseable