This class provides a simple alternative to FileWriter
that will use a lock file to prevent duplicate writes.
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.
| Modifier and Type | Field and Description |
|---|---|
| private static final String | LCK
The extension for the lock file. |
| private final File | lockFile
The lock file. |
| private final Writer | out
The writer to decorate. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | LockableFileWriter(String
file to write to, not null fileName, boolean true if content should be appended, false to overwrite append)Constructs a LockableFileWriter. |
| public | LockableFileWriter(String
the file to write to, not null fileName, boolean true if content should be appended, false to overwrite append, String the directory in which the lock file should be held lockDir)Constructs a LockableFileWriter. |
| public | |
| public | LockableFileWriter(File
the file to write to, not null file, boolean true if content should be appended, false to overwrite append)Constructs a LockableFileWriter. |
| public | LockableFileWriter(File
the file to write to, not null file, boolean true if content should be appended, false to overwrite append, String the directory in which the lock file should be held lockDir)Constructs a LockableFileWriter. |
| public | LockableFileWriter(File
the file to write to, not null file, String the encoding to use, null means platform default encoding)Constructs a LockableFileWriter with a file encoding. |
| public | LockableFileWriter(File
the file to write to, not null file, String the encoding to use, null means platform default encoding, boolean true if content should be appended, false to overwrite append, String the directory in which the lock file should be held lockDir)Constructs a LockableFileWriter with a file encoding. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements abstract java. Implements java. |
| private void | |
| public void | flush()
Implements abstract java. Implements java. |
| private Writer | Returns: The initialised writerthe file to be accessed file, String the encoding to use encoding, boolean true to append append)Initialise the wrapped file writer. |
| private void | testLockDir(File
the File representing the lock directory lockDir)Tests that we can write to the lock directory. |
| public void | write(int
int specifying a character to be written idx)Overrides java. |
| public void | write(char[]
Array of characters to be written chr)Overrides java. |
| public void | write(char[]
Array of characters chr, int Offset from which to start writing characters st, int Number of characters to write end)Implements abstract java. |
| public void | |
| public void | write(String
A String str, int Offset from which to start writing characters st, int Number of characters to write end)Overrides java. |
| LCK | back to summary |
|---|---|
| private static final String LCK The extension for the lock file. | |
| lockFile | back to summary |
|---|---|
| private final File lockFile The lock file. | |
| out | back to summary |
|---|---|
| private final Writer out The writer to decorate. | |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(String fileName) throws IOException Constructs a LockableFileWriter. If the file exists, it is overwritten.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(String fileName, boolean append) throws IOException Constructs a LockableFileWriter.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(String fileName, boolean append, String lockDir) throws IOException Constructs a LockableFileWriter.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(File file) throws IOException Constructs a LockableFileWriter. If the file exists, it is overwritten.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(File file, boolean append) throws IOException Constructs a LockableFileWriter.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(File file, boolean append, String lockDir) throws IOException Constructs a LockableFileWriter.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(File file, String encoding) throws IOException Constructs a LockableFileWriter with a file encoding.
| |
| LockableFileWriter | back to summary |
|---|---|
| public LockableFileWriter(File file, String encoding, boolean append, String lockDir) throws IOException Constructs a LockableFileWriter with a file encoding.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements abstract java. Implements java. Closes the file writer.
| |
| createLock | back to summary |
|---|---|
| private void createLock() throws IOException Creates the lock file.
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Implements abstract java. Implements java. Doc from java. Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
| |
| initWriter | back to summary |
|---|---|
| private Writer initWriter(File file, String encoding, boolean append) throws IOException Initialise the wrapped file writer. Ensure that a cleanup occurs if the writer creation fails.
| |
| testLockDir | back to summary |
|---|---|
| private void testLockDir(File lockDir) throws IOException Tests that we can write to the lock directory.
| |
| write | back to summary |
|---|---|
| public void write(int idx) throws IOException Overrides java. Doc from java. Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored. Subclasses that intend to support efficient single-character output should override this method.
| |
| write | back to summary |
|---|---|
| public void write(char[] chr) throws IOException Overrides java. Doc from java. Writes an array of characters.
| |
| write | back to summary |
|---|---|
| public void write(char[] chr, int st, int end) throws IOException Implements abstract java. Doc from java. Writes a portion of an array of characters.
| |
| write | back to summary |
|---|---|
| public void write(String str) throws IOException Overrides java. Doc from java. Writes a string.
| |
| write | back to summary |
|---|---|
| public void write(String str, int st, int end) throws IOException Overrides java. Doc from java. Writes a portion of a string.
| |