org.apache.commons.io.output

Class LockableFileWriter

public class LockableFileWriter extends Writer

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.

Version: $Id: LockableFileWriter.java 610010 2008-01-08 14:50:59Z niallp $

Author: Scott Sanders Michael Salmon Jon S. Stevens Daniel Rall Stephen Colebourne Andy Lehane

Constructor Summary
LockableFileWriter(String fileName)
Constructs a LockableFileWriter.
LockableFileWriter(String fileName, boolean append)
Constructs a LockableFileWriter.
LockableFileWriter(String fileName, boolean append, String lockDir)
Constructs a LockableFileWriter.
LockableFileWriter(File file)
Constructs a LockableFileWriter.
LockableFileWriter(File file, boolean append)
Constructs a LockableFileWriter.
LockableFileWriter(File file, boolean append, String lockDir)
Constructs a LockableFileWriter.
LockableFileWriter(File file, String encoding)
Constructs a LockableFileWriter with a file encoding.
LockableFileWriter(File file, String encoding, boolean append, String lockDir)
Constructs a LockableFileWriter with a file encoding.
Method Summary
voidclose()
Closes the file writer.
voidflush()
Flush the stream.
voidwrite(int idx)
Write a character.
voidwrite(char[] chr)
Write the characters from an array.
voidwrite(char[] chr, int st, int end)
Write the specified characters from an array.
voidwrite(String str)
Write the characters from a string.
voidwrite(String str, int st, int end)
Write the specified characters from a string.

Constructor Detail

LockableFileWriter

public LockableFileWriter(String fileName)
Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters: fileName the file to write to, not null

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

LockableFileWriter

public LockableFileWriter(String fileName, boolean append)
Constructs a LockableFileWriter.

Parameters: fileName file to write to, not null append true if content should be appended, false to overwrite

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

LockableFileWriter

public LockableFileWriter(String fileName, boolean append, String lockDir)
Constructs a LockableFileWriter.

Parameters: fileName the file to write to, not null append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

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

LockableFileWriter

public LockableFileWriter(File file)
Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters: file the file to write to, not null

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

LockableFileWriter

public LockableFileWriter(File file, boolean append)
Constructs a LockableFileWriter.

Parameters: file the file to write to, not null append true if content should be appended, false to overwrite

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

LockableFileWriter

public LockableFileWriter(File file, boolean append, String lockDir)
Constructs a LockableFileWriter.

Parameters: file the file to write to, not null append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

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

LockableFileWriter

public LockableFileWriter(File file, String encoding)
Constructs a LockableFileWriter with a file encoding.

Parameters: file the file to write to, not null encoding the encoding to use, null means platform default

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

LockableFileWriter

public LockableFileWriter(File file, String encoding, boolean append, String lockDir)
Constructs a LockableFileWriter with a file encoding.

Parameters: file the file to write to, not null encoding the encoding to use, null means platform default append true if content should be appended, false to overwrite lockDir the directory in which the lock file should be held

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

Method Detail

close

public void close()
Closes the file writer.

Throws: IOException if an I/O error occurs

flush

public void flush()
Flush the stream.

Throws: IOException if an I/O error occurs

write

public void write(int idx)
Write a character.

Parameters: idx the character to write

Throws: IOException if an I/O error occurs

write

public void write(char[] chr)
Write the characters from an array.

Parameters: chr the characters to write

Throws: IOException if an I/O error occurs

write

public void write(char[] chr, int st, int end)
Write the specified characters from an array.

Parameters: chr the characters to write st The start offset end The number of characters to write

Throws: IOException if an I/O error occurs

write

public void write(String str)
Write the characters from a string.

Parameters: str the string to write

Throws: IOException if an I/O error occurs

write

public void write(String str, int st, int end)
Write the specified characters from a string.

Parameters: str the string to write st The start offset end The number of characters to write

Throws: IOException if an I/O error occurs

Copyright © 2002-2010 Apache Software Foundation. All Rights Reserved.