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.
N.B. the lock file is deleted when close()
is called
- or if the main file cannot be opened initially.
In the (unlikely) event that the lockfile cannot be deleted,
this is not reported, and subsequent requests using
the same lockfile will fail.
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.
Constructor and Description |
---|
LockableFileWriter(java.io.File file)
Constructs a LockableFileWriter.
|
LockableFileWriter(java.io.File file,
boolean append)
Constructs a LockableFileWriter.
|
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 encoding)
Constructs a LockableFileWriter with a file encoding.
|
LockableFileWriter(java.io.File file,
java.nio.charset.Charset encoding,
boolean append,
java.lang.String lockDir)
Constructs a LockableFileWriter with a file encoding.
|
LockableFileWriter(java.io.File file,
java.lang.String encoding)
Constructs a LockableFileWriter with a file encoding.
|
LockableFileWriter(java.io.File file,
java.lang.String encoding,
boolean append,
java.lang.String lockDir)
Constructs a LockableFileWriter with a file encoding.
|
LockableFileWriter(java.lang.String fileName)
Constructs a LockableFileWriter.
|
LockableFileWriter(java.lang.String fileName,
boolean append)
Constructs a LockableFileWriter.
|
LockableFileWriter(java.lang.String fileName,
boolean append,
java.lang.String lockDir)
Constructs a LockableFileWriter.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the file writer and deletes the lockfile (if possible).
|
void |
flush()
Flush the stream.
|
void |
write(char[] chr)
Write the characters from an array.
|
void |
write(char[] chr,
int st,
int end)
Write the specified characters from an array.
|
void |
write(int idx)
Write a character.
|
void |
write(java.lang.String str)
Write the characters from a string.
|
void |
write(java.lang.String str,
int st,
int end)
Write the specified characters from a string.
|
public LockableFileWriter(java.lang.String fileName) throws java.io.IOException
fileName
- the file to write to, not nulljava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorpublic LockableFileWriter(java.lang.String fileName, boolean append) throws java.io.IOException
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 errorpublic LockableFileWriter(java.lang.String fileName, boolean append, java.lang.String lockDir) throws java.io.IOException
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 LockableFileWriter(java.io.File file) throws java.io.IOException
file
- the file to write to, not nulljava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorpublic LockableFileWriter(java.io.File file, boolean append) throws java.io.IOException
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 errorpublic LockableFileWriter(java.io.File file, java.nio.charset.Charset encoding) throws java.io.IOException
file
- the file to write to, not nullencoding
- the encoding to use, null means platform defaultjava.lang.NullPointerException
- if the file is nulljava.io.IOException
- in case of an I/O errorpublic LockableFileWriter(java.io.File file, java.lang.String encoding) throws java.io.IOException
file
- the file to write to, not nullencoding
- the encoding to use, 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.public LockableFileWriter(java.io.File file, java.nio.charset.Charset encoding, boolean append, java.lang.String lockDir) throws java.io.IOException
file
- the file to write to, not nullencoding
- 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 errorpublic LockableFileWriter(java.io.File file, java.lang.String encoding, boolean append, java.lang.String lockDir) throws java.io.IOException
file
- the file to write to, not nullencoding
- 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.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 occurspublic void write(int idx) throws java.io.IOException
write
in class java.io.Writer
idx
- the character to writejava.io.IOException
- if an I/O error occurspublic void write(char[] chr) throws java.io.IOException
write
in class java.io.Writer
chr
- the characters to writejava.io.IOException
- if an I/O error occurspublic void write(char[] chr, int st, int end) throws java.io.IOException
write
in class java.io.Writer
chr
- the characters to writest
- The start offsetend
- The number of characters to writejava.io.IOException
- if an I/O error occurspublic 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 occurspublic void write(java.lang.String str, int st, int end) throws java.io.IOException
write
in class java.io.Writer
str
- the string to writest
- The start offsetend
- The number of characters to writejava.io.IOException
- if an I/O error occurspublic 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 occursCopyright © 2010 - 2020 Adobe. All Rights Reserved