public class LockableFileWriter extends Writer
This class provides a simple alternative to FileWriter that will use a lock file to prevent duplicate writes.
Note: The lock file is deleted when close() is called - or if the main file cannot be opened initially. In the (unlikely) event
that the lock file cannot be deleted, an exception is thrown.
By default, the file will be overwritten, but this may be changed to append. The lock directory may be specified, but defaults to the system property
java.io.tmpdir. The encoding may also be specified, and defaults to the platform default.
To build an instance, use LockableFileWriter.Builder.
LockableFileWriter.Builder| Modifier and Type | Class and Description |
|---|---|
static class |
LockableFileWriter.Builder
Builds a new
LockableFileWriter. |
| Modifier and Type | Method and Description |
|---|---|
static LockableFileWriter.Builder |
builder()
Constructs a new
LockableFileWriter.Builder. |
void |
close()
Closes the file writer and deletes the lock file.
|
void |
flush()
Flushes the stream.
|
void |
write(char[] cbuf)
Writes the characters from an array.
|
void |
write(char[] cbuf,
int off,
int len)
Writes the specified characters from an array.
|
void |
write(int c)
Writes a character.
|
void |
write(String str)
Writes the characters from a string.
|
void |
write(String str,
int off,
int len)
Writes the specified characters from a string.
|
public static LockableFileWriter.Builder builder()
LockableFileWriter.Builder.LockableFileWriter.Builder.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class WriterIOException - if an I/O error occurs.public void flush()
throws IOException
flush in interface Flushableflush in class WriterIOException - if an I/O error occurs.public void write(char[] cbuf)
throws IOException
write in class Writercbuf - the characters to writeIOException - if an I/O error occurs.public void write(char[] cbuf,
int off,
int len)
throws IOException
write in class Writercbuf - the characters to writeoff - The start offsetlen - The number of characters to writeIOException - if an I/O error occurs.public void write(int c)
throws IOException
write in class Writerc - the character to writeIOException - if an I/O error occurs.public void write(String str) throws IOException
write in class Writerstr - the string to writeIOException - if an I/O error occurs.public void write(String str, int off, int len) throws IOException
write in class Writerstr - the string to writeoff - The start offsetlen - The number of characters to writeIOException - if an I/O error occurs.