public class LockableFileWriter
extends java.io.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, see LockableFileWriter.Builder
.
Modifier and Type | Class and Description |
---|---|
static class |
LockableFileWriter.Builder
Builds a new
LockableFileWriter instance. |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
LCK
The extension for the lock file.
|
private java.io.File |
lockFile
The lock file.
|
private java.io.Writer |
out
The writer to decorate.
|
Constructor and Description |
---|
LockableFileWriter(java.io.File file)
Deprecated.
|
LockableFileWriter(java.io.File file,
boolean append)
Deprecated.
|
LockableFileWriter(java.io.File file,
boolean append,
java.lang.String lockDir)
Deprecated.
2.5 use
LockableFileWriter(File, Charset, boolean, String) instead |
LockableFileWriter(java.io.File file,
java.nio.charset.Charset charset)
Deprecated.
|
LockableFileWriter(java.io.File file,
java.nio.charset.Charset charset,
boolean append,
java.lang.String lockDir)
Deprecated.
|
LockableFileWriter(java.io.File file,
java.lang.String charsetName)
Deprecated.
|
LockableFileWriter(java.io.File file,
java.lang.String charsetName,
boolean append,
java.lang.String lockDir)
Deprecated.
|
LockableFileWriter(java.lang.String fileName)
Deprecated.
|
LockableFileWriter(java.lang.String fileName,
boolean append)
Deprecated.
|
LockableFileWriter(java.lang.String fileName,
boolean append,
java.lang.String lockDir)
Deprecated.
|
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.
|
private void |
createLock()
Creates the lock file.
|
void |
flush()
Flushes the stream.
|
private java.io.Writer |
initWriter(java.io.File file,
java.nio.charset.Charset charset,
boolean append)
Initializes the wrapped file writer.
|
private void |
testLockDir(java.io.File lockDir)
Tests that we can write to the lock directory.
|
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(java.lang.String str)
Writes the characters from a string.
|
void |
write(java.lang.String str,
int off,
int len)
Writes the specified characters from a string.
|
private static final java.lang.String LCK
private final java.io.Writer out
private final java.io.File lockFile
@Deprecated public LockableFileWriter(java.io.File file) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nulljava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.io.File file, boolean append) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nullappend
- true if content should be appended, false to overwritejava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.io.File file, boolean append, java.lang.String lockDir) throws java.io.IOException
LockableFileWriter(File, Charset, boolean, String)
insteadfile
- the file to write to, not nullappend
- true if content should be appended, false to overwritelockDir
- the directory in which the lock file should be heldjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nullcharset
- the charset to use, null means platform defaultjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.io.File file, java.nio.charset.Charset charset, boolean append, java.lang.String lockDir) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nullcharset
- the name of the requested charset, null means platform defaultappend
- true if content should be appended, false to overwritelockDir
- the directory in which the lock file should be heldjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.io.File file, java.lang.String charsetName) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nullcharsetName
- the name of the requested charset, null means platform defaultjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorjava.nio.charset.UnsupportedCharsetException
- thrown instead of UnsupportedEncodingException
in version 2.2 if the encoding is not
supported.@Deprecated public LockableFileWriter(java.io.File file, java.lang.String charsetName, boolean append, java.lang.String lockDir) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
file
- the file to write to, not nullcharsetName
- the encoding to use, null means platform defaultappend
- true if content should be appended, false to overwritelockDir
- the directory in which the lock file should be heldjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorjava.nio.charset.UnsupportedCharsetException
- thrown instead of UnsupportedEncodingException
in version 2.2 if the encoding is not
supported.@Deprecated public LockableFileWriter(java.lang.String fileName) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
fileName
- the file to write to, not nulljava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.lang.String fileName, boolean append) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
fileName
- file to write to, not nullappend
- true if content should be appended, false to overwritejava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O error@Deprecated public LockableFileWriter(java.lang.String fileName, boolean append, java.lang.String lockDir) throws java.io.IOException
builder()
, LockableFileWriter.Builder
, and LockableFileWriter.Builder.get()
fileName
- the file to write to, not nullappend
- true if content should be appended, false to overwritelockDir
- the directory in which the lock file should be heldjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorpublic static LockableFileWriter.Builder builder()
LockableFileWriter.Builder
.LockableFileWriter.Builder
.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
- if an I/O error occurs.private void createLock() throws java.io.IOException
java.io.IOException
- if we cannot create the filepublic void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
- if an I/O error occurs.private java.io.Writer initWriter(java.io.File file, java.nio.charset.Charset charset, boolean append) throws java.io.IOException
file
- the file to be accessedcharset
- the charset to useappend
- true to appendjava.io.IOException
- if an error occursprivate void testLockDir(java.io.File lockDir) throws java.io.IOException
lockDir
- the File representing the lock directoryjava.io.IOException
- if we cannot write to the lock directoryjava.io.IOException
- if we cannot find the lock filepublic void write(char[] cbuf) throws java.io.IOException
write
in class java.io.Writer
cbuf
- the characters to writejava.io.IOException
- if an I/O error occurs.public void write(char[] cbuf, int off, int len) throws java.io.IOException
write
in class java.io.Writer
cbuf
- the characters to writeoff
- The start offsetlen
- The number of characters to writejava.io.IOException
- if an I/O error occurs.public void write(int c) throws java.io.IOException
write
in class java.io.Writer
c
- the character to writejava.io.IOException
- if an I/O error occurs.public void write(java.lang.String str) throws java.io.IOException
write
in class java.io.Writer
str
- the string to writejava.io.IOException
- if an I/O error occurs.public void write(java.lang.String str, int off, int len) throws java.io.IOException
write
in class java.io.Writer
str
- the string to writeoff
- The start offsetlen
- The number of characters to writejava.io.IOException
- if an I/O error occurs.