org.apache.commons.io.output
Class LockableFileWriter

java.lang.Object
  extended byjava.io.Writer
      extended byorg.apache.commons.io.output.LockableFileWriter

public class LockableFileWriter
extends java.io.Writer

FileWriter that will create and honor lock files to allow simple cross thread file lock handling. If Writer attributes are unspecified, the default behavior is to overwrite (rather than to append), and to use the value of the system property java.io.tmpdir for the lock file directory.

Version:
$Id: LockableFileWriter.java,v 1.6 2003/12/30 15:19:59 jeremias Exp $
Author:
Scott Sanders, Michael Salmon, Jon S. Stevens, Daniel Rall

Field Summary
private  boolean append
           
private static java.lang.String LCK
           
private  java.io.File lockFile
           
private  java.io.FileWriter writer
           
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
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)
          Constructs a LockableFileWriter.
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.
 
Method Summary
 void close()
           
private  void createLock()
           
 void flush()
           
private  void testLockDir(java.io.File lockDir)
           
 void write(char[] cbuf, int off, int len)
           
 
Methods inherited from class java.io.Writer
write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LCK

private static final java.lang.String LCK
See Also:
Constant Field Values

lockFile

private java.io.File lockFile

writer

private java.io.FileWriter writer

append

private boolean append
Constructor Detail

LockableFileWriter

public LockableFileWriter(java.lang.String fileName)
                   throws java.io.IOException
Constructs a LockableFileWriter. If the file exists, it is overwritten.

Parameters:
fileName - file to write to
Throws:
java.io.IOException - in case of an I/O error

LockableFileWriter

public LockableFileWriter(java.lang.String fileName,
                          boolean append)
                   throws java.io.IOException
Constructs a LockableFileWriter.

Parameters:
fileName - file to write to
append - true if content should be appended (default is to overwrite).
Throws:
java.io.IOException - in case of an I/O error

LockableFileWriter

public LockableFileWriter(java.lang.String fileName,
                          boolean append,
                          java.lang.String lockDir)
                   throws java.io.IOException
Constructs a LockableFileWriter.

Parameters:
fileName - file to write to
append - true if content should be appended (default is to overwrite).
lockDir - Specifies the directory in which the lock file should be held.
Throws:
java.io.IOException - in case of an I/O error

LockableFileWriter

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

Parameters:
file - file to write to
Throws:
java.io.IOException - in case of an I/O error

LockableFileWriter

public LockableFileWriter(java.io.File file,
                          boolean append)
                   throws java.io.IOException
Constructs a LockableFileWriter.

Parameters:
file - file to write to
append - true if content should be appended (default is to overwrite).
Throws:
java.io.IOException - in case of an I/O error

LockableFileWriter

public LockableFileWriter(java.io.File file,
                          boolean append,
                          java.lang.String lockDir)
                   throws java.io.IOException
Constructs a LockableFileWriter.

Parameters:
file - file to write to
append - true if content should be appended (default is to overwrite).
lockDir - Specifies the directory in which the lock file should be held.
Throws:
java.io.IOException - in case of an I/O error
Method Detail

testLockDir

private void testLockDir(java.io.File lockDir)
                  throws java.io.IOException
Throws:
java.io.IOException

createLock

private void createLock()
                 throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Throws:
java.io.IOException
See Also:
Writer.close()

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Throws:
java.io.IOException
See Also:
Writer.write(char[], int, int)

flush

public void flush()
           throws java.io.IOException
Throws:
java.io.IOException
See Also:
Writer.flush()