Top Description Fields Constructors Methods
org.apache.commons.io.output

public Class LockableFileWriter

extends Writer
Class Inheritance
Imports
java.io.File, .FileOutputStream, .FileWriter, .IOException, .OutputStream, .OutputStreamWriter, .Writer, org.apache.commons.io.FileUtils, .IOUtils

FileWriter that will create and honor lock files to allow simple cross thread file lock handling.

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.

Authors
Scott Sanders, Michael Salmon, Jon S. Stevens, Daniel Rall, Stephen Colebourne, Andy Lehane
Version
$Id: LockableFileWriter.java 437567 2006-08-28 06:39:07Z bayard $

Field Summary

Modifier and TypeField 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.

Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
public
LockableFileWriter(String
the file to write to, not null
fileName
)

Constructs a LockableFileWriter.

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
LockableFileWriter(File
the file to write to, not null
file
)

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
)

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.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the file writer.
private void
createLock()

Creates the lock file.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.
private Writer

Returns:

The initialised writer
initWriter
(File
the 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.io.Writer.write.

Writes a single character.
public void
write(char[]
Array of characters to be written
chr
)

Overrides java.io.Writer.write.

Writes an array of characters.
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.io.Writer.write.

Writes a portion of an array of characters.
public void
write(String
String to be written
str
)

Overrides java.io.Writer.write.

Writes a string.
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.io.Writer.write.

Writes a portion of a string.
Inherited from java.io.Writer:
appendappendappendnullWriter

Field Detail

LCKback to summary
private static final String LCK

The extension for the lock file.

lockFileback to summary
private final File lockFile

The lock file.

outback to summary
private final Writer out

The writer to decorate.

Constructor Detail

LockableFileWriterback to summary
public LockableFileWriter(String fileName) throws IOException

Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters
fileName:String

the file to write to, not null

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(String fileName, boolean append) throws IOException

Constructs a LockableFileWriter.

Parameters
fileName:String

file to write to, not null

append:boolean

true if content should be appended, false to overwrite

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(String fileName, boolean append, String lockDir) throws IOException

Constructs a LockableFileWriter.

Parameters
fileName:String

the file to write to, not null

append:boolean

true if content should be appended, false to overwrite

lockDir:String

the directory in which the lock file should be held

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(File file) throws IOException

Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters
file:File

the file to write to, not null

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(File file, boolean append) throws IOException

Constructs a LockableFileWriter.

Parameters
file:File

the file to write to, not null

append:boolean

true if content should be appended, false to overwrite

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(File file, boolean append, String lockDir) throws IOException

Constructs a LockableFileWriter.

Parameters
file:File

the file to write to, not null

append:boolean

true if content should be appended, false to overwrite

lockDir:String

the directory in which the lock file should be held

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(File file, String encoding) throws IOException

Constructs a LockableFileWriter with a file encoding.

Parameters
file:File

the file to write to, not null

encoding:String

the encoding to use, null means platform default

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null
LockableFileWriterback to summary
public LockableFileWriter(File file, String encoding, boolean append, String lockDir) throws IOException

Constructs a LockableFileWriter with a file encoding.

Parameters
file:File

the file to write to, not null

encoding:String

the encoding to use, null means platform default

append:boolean

true if content should be appended, false to overwrite

lockDir:String

the directory in which the lock file should be held

Exceptions
IOException:
in case of an I/O error
NullPointerException:
if the file is null

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the file writer.

Exceptions
IOException:
if an I/O error occurs
createLockback to summary
private void createLock() throws IOException

Creates the lock file.

Exceptions
IOException:
if we cannot create the file
flushback to summary
public void flush() throws IOException

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Doc from java.io.Writer.flush.

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.

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#flush()
initWriterback 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.

Parameters
file:File

the file to be accessed

encoding:String

the encoding to use

append:boolean

true to append

Returns:Writer

The initialised writer

Exceptions
IOException:
if an error occurs
testLockDirback to summary
private void testLockDir(File lockDir) throws IOException

Tests that we can write to the lock directory.

Parameters
lockDir:File

the File representing the lock directory

Exceptions
IOException:
  • if we cannot write to the lock directory
  • if we cannot find the lock file
writeback to summary
public void write(int idx) throws IOException

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

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.

Parameters
idx:int

int specifying a character to be written

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#write(int)
writeback to summary
public void write(char[] chr) throws IOException

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes an array of characters.

Parameters
chr:char[]

Array of characters to be written

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#write(char[])
writeback to summary
public void write(char[] chr, int st, int end) throws IOException

Implements abstract java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of an array of characters.

Parameters
chr:char[]

Array of characters

st:int

Offset from which to start writing characters

end:int

Number of characters to write

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#write(char[], int, int)
writeback to summary
public void write(String str) throws IOException

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a string.

Parameters
str:String

String to be written

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#write(String)
writeback to summary
public void write(String str, int st, int end) throws IOException

Overrides java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of a string.

Parameters
str:String

A String

st:int

Offset from which to start writing characters

end:int

Number of characters to write

Exceptions
IOException:
If an I/O error occurs
See Also
java.io.Writer#write(String, int, int)